1

This server acknowledges the PHP connection, but the values entered in the form do not get stored just empty table rows.

The PHP code:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Aliens Abducted Me - Report an Abduction</title>
</head>
<body>
  <h2>Aliens Abducted Me - Report an Abduction</h2>

<?php


  $name = $_POST'firstname';
  $last_name =$_POST'lastname';
  $when_it_happened = $_POST'whenithappened';
  $how_long = $_POST'howlong';
  $how_many = $_POST'howmany';
  $alien_description = $_POST'aliendescription';
  $what_they_did = $_POST'whattheydid';
  $fang_spotted = $_POST'fangspotted';
  $email = $_POST'email';
  $other = $_POST'other';

  $dbc = mysqli_connect('localhost', 'root', 'Password', 'aliendatabase')
    or die('Error Connecting to MySQL server');

  $query = "INSERT INTO aliens_abduction (first_name, last_name, when_it_happened, how_long, " .
    "how_many, alien_description, what_they_did, fang_spotted, other, email) " .
    "VALUES ('$first_name, '$last_name', '$when_it_happened', '$how_long', '$how_many', " .
    "'$alien_description', '$what_they_did', '$fang_spotted', '$other', '$email')";

  $result = mysqli_query($dbc, $query)
    or die('Error Querying Database');

  mysqli_close($dbc); 

  echo 'Thanks for submitting the form.<br />';
  echo 'You were abducted ' . $when_it_happened;
  echo ' and were gone for ' . $how_long . '<br />';
  echo 'Number of aliens: ' . $how_many . '<br />';
  echo 'Describe them: ' . $alien_description . '<br />';
  echo 'The aliens did this: ' . $what_they_did . '<br />';
  echo 'Was Fang there? ' . $fang_spotted . '<br />';
  echo 'Other comments: ' . $other . '<br />';
  echo 'Your email address is ' . $email;

?>

</body>
</html>

Here's the Form/HTML Code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Aliens Abducted Me - Report an Abduction</title>
  <link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
  <h2>Aliens Abducted Me - Report an Abduction</h2>

  <p>Share your story of alien abduction:</p>
  <form method="post" action="report.php">
    <label for="firstname">First name:</label>
    <input type="text" id="firstname" name="firstname" /><br />
    <label for="lastname">Last name:</label>
    <input type="text" id="lastname" name="lastname" /><br />
    <label for="email">What is your email address?</label>
    <input type="text" id="email" name="email" /><br />
    <label for="whenithappened">When did it happen?</label>
    <input type="text" id="whenithappened" name="whenithappened" /><br />
    <label for="howlong">How long were you gone?</label>
    <input type="text" id="howlong" name="howlong" /><br />
    <label for="howmany">How many did you see?</label>
    <input type="text" id="howmany" name="howmany" /><br />
    <label for="aliendescription">Describe them:</label>
    <input type="text" id="aliendescription" name="aliendescription" size="32" /><br />
    <label for="whattheydid">What did they do to you?</label>
    <input type="text" id="whattheydid" name="whattheydid" size="32" /><br />
    <label for="fangspotted">Have you seen my dog Fang?</label>
    Yes <input id="fangspotted" name="fangspotted" type="radio" value="yes" />
    No <input id="fangspotted" name="fangspotted" type="radio" value="no" /><br />
    <img src="fang.jpg" width="100" height="175"
      alt="My abducted dog Fang." /><br />
    <label for="other">Anything else you want to add?</label>
    <textarea id="other" name="other"></textarea><br />
    <input type="submit" value="Report Abduction" name="submit" />
  </form>
</body>
</html>

The code is straight from a book. php v5.6.22 and latest Apache and MySQL. Can anyone tell me what might be wrong or what I'm doing wrong

Mr A
  • 7
  • 6

1 Answers1

3

See this => $_POST'firstname'; and all the other POST arrays?

They are missing brackets [] for them.

So change that to $_POST['firstname']; and do the same for all the others also.

Error reporting would have helped you here, as would reading the manual on forms.

Plus, you should use a conditional !empty() against all your POST arrays going in your database.

This could trigger errors if any of those are left empty, should your database not accept NULL values.

You also have a missing quote for ('$first_name, and concatenates in your query.

This needed to be rewritten as such:

$query = "INSERT INTO aliens_abduction (first_name, last_name, when_it_happened, how_long, 
    how_many, alien_description, what_they_did, fang_spotted, other, email) 
    VALUES ('$first_name', '$last_name', '$when_it_happened', '$how_long', '$how_many', 
    '$alien_description', '$what_they_did', '$fang_spotted', '$other', '$email')";

Footnotes:

Your present code is open to SQL injection. Use prepared statements, or PDO with prepared statements.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Error reporting doesn't help when there are syntax errors, since nothing in the script will run. – Barmar Jun 07 '16 at 00:54
  • @Barmar Have I missed something? – Funk Forty Niner Jun 07 '16 at 00:54
  • @Barmar Ok, I spotted more now. Lordie, I think I'll have to rewrite that whole line. – Funk Forty Niner Jun 07 '16 at 00:56
  • `$_POST'firstname'` is a syntax error, so the script won't run, so it can't execute `error_reporting(E_ALL);`. – Barmar Jun 07 '16 at 00:56
  • @Barmar Oh interesting, I didn't know that till now. One never ceases to learn something, which is the beauty of what I get to learn from you and other Stack members. Thanks – Funk Forty Niner Jun 07 '16 at 00:58
  • Thank you missing the brackets, did that but still not working – Mr A Jun 07 '16 at 00:58
  • http://prntscr.com/bd8y1m here, the filled in blocked is me sending without form. – Mr A Jun 07 '16 at 00:59
  • http://prntscr.com/bd8zri this is the result, i dont think i need error handler just to send data to a server, simple data – Mr A Jun 07 '16 at 01:06
  • @MrA you don't seem to be running as PHP here. That output shot you gave me, shows variables rather than being parsed. How are you running this as, `file:///file.xxx` or as `http://localhost/file.php`? Two different animals here. – Funk Forty Niner Jun 07 '16 at 01:12
  • the files suppose to be in the apache folder? – Mr A Jun 07 '16 at 01:19
  • @MrA depending on your webserver/php/apache installation and is beyond the scope of the question asked. You will need to consult the documentation for it and then access that file as `http://localhost/file.xxx`. – Funk Forty Niner Jun 07 '16 at 01:23
  • It actually worked now, my fault. Thank you so helpful. i didn't have the directory in the root folder of Apache. silly mistakes cause me days of trouble – Mr A Jun 07 '16 at 01:44
  • @MrA No worries. I'm glad to hear that everything worked out, *cheers!* – Funk Forty Niner Jun 07 '16 at 01:49