无法使用$ _POST将数据插入MySQL数据库

无法使用$ _POST将数据插入MySQL数据库

问题描述:

No data shows up in my database when I run the code below.

I keep getting the "Can't write to the database" error.

Here is a image of my database setup, with data entered manually through phpmyadmin.

Here is another image with my table struture.

HTML form

  <form method="post" action="report.php">
    <label for="firstname">First name:</label>
    <input type="text" id="firstname" name="firstname" placeholder="test"/><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>

report.php

<?php
$name = $_POST['firstname'] . ' ' . $_POST['lastname'];
$first_name = $_POST['firstname'];
$last_name = $_POST['lastname'];
$when_it_happened = $_POST['whenithappened'];
$how_long = $_POST['howlong'];
$how_many = $_POST['howmany'];
$what_they_did = $_POST['whattheydid'];
$alien_description = $_POST['aliendescription'];
$fang_spotted = $_POST['fangspotted'];
$other = $_POST['other'];
$email = $_POST['email'];
$to = 'test@gmail.com';
$subject = 'Aliens abducted med - Abduction report';
$msg = "$name was abducted $when_it_happened and was gone for $how_long 
" . 
  "Number of aliens: $how_many
" . 
  "Alien description: $alien_description
" . 
  "What they did: $what_they_did
" . 
  "Fang spotted: $fang_spotted
" . 
  "Other comments: $other";

$dbc = mysqli_connect('localhost', 'root', '', 'phpheadfirst')
  or die ('Error. Can\'t connect to the databse');

  $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. Can\'t write to the database');

mysqli_close($dbc);

echo 'Hello ' . $name . ', and thanks for submitting the form.' . '<br />';
echo 'You were abducted ' . $when_it_happened;
echo ' by ' . $how_many . ' aliens';
echo ' and were gone for ' . $how_long . '<br />';
echo 'What did they do to you? ' . $what_they_did . '<br />';
echo 'Describe them: ' . $alien_description . '<br />';
echo 'Was fang there? ' . $fang_spotted . '<br />';
echo 'Do you have more to add?' . $other . '<br />';
echo 'Your email adress is ' . $email;

mail($to, $subject, $msg, 'From:' . $email);

I have tried to find the problem, but I can't.

I am using the newest XAMPP install and it all works.

Have also tried the code on my online webserver - still the same issue.

I found the answer. One of my column in my table had a "d" to much in it. Found the error with the help from @Marc B

There are too many " in your INSERT query... It should be like this..

"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')";

Instead of a fixed (and totally useless) error message, have MySQL TELL you what went wrong:

$result = mysqli_query($dbc, $query) or die(mysqli_error($dbc));
                                            ^^^^^^^^^^^^^^^^^^

You'll probably find it due to a syntax error, caused by your gaping wide open sql injection attack vulnerabilities.

$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')";

Should become

$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 . "')";