0

I am trying to store form data into database using mysqli but it is generating query error my code is given below.... When ever I try to submit the database connection is generating.. the $_POST is working perfectly.. the error only generating by mysqli_query..

<?php 
$name = $_POST["firstname"] . " " . $_POST["lastname"];
$email = $_POST["email"];
$happen = $_POST["whendidhappen"];
$howlong = $_POST["howlong"];
$howmany = $_POST["howmany"];
$describe = $_POST["describe"];
$whattheydid= $_POST["whattheydid"];
$seenmycat = $_POST["seenmycat"];
$anythingelse = $_POST["anythingelse"];


$dbc = mysqli_connect('localhost','root','','abductionreport') 
or die('Database connection error');


$query = "INSERT INTO abductionform (firstname, lastname, email,whendidhappen, howlong, describe, whattheydid, seenmycat,anythingelse)VALUES('$name','$name','$email','$happen','$howlong', '$howmany','$describe','$whattheydid', '$seenmycat','$anythingelse')";



$result = mysqli_query($dbc,$query) or die ("Query Error");

mysqli_close($dbc); 



?>
<h3>Aliens Abducted Me - Report an Abduction</h3>
<p>Thanks for Submiting the form.</p>
<?php 
echo "$name it happend to you on $happen it take $howlong <br>";
echo "Number of aliens: $howmany<br>";
echo "Describe: $describe<br>";
echo "What they did to you: $whattheydid<br>";
echo "Have you seen my cat: $seenmycat<br>";
echo "Anything else : $anythingelse<br>";
echo "Your Email Address is : $email<br>";

?>
Federkun
  • 36,084
  • 8
  • 78
  • 90

1 Answers1

0

DESCRIBE is a mysql keyword. Wrap the column name in backticks. For that matter wrap your table and all columns in backticks. Always check that $_POST elements exist with isset () before trying to access them. Use mysqli prepared statements with placeholders for improved security. Always perform error checking BEFORE posting to SO.

You also have 9 columns and 10 values in your query - this will cause a failure every time.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • thanks I have found solution https://youtu.be/PIwjucN_CHY – Ghufran Khalil Aug 31 '17 at 14:57
  • @GhufranKhalil Why are you promoting that video? It is easier (less vague) to say what fixed it rather than force people to link-chase. If my answer didn't solve your issue, you don't have to award the green tick, but posting youtube links seems pretty spammy. – mickmackusa Aug 31 '17 at 15:08