List Only

Simple SQL/PHP - INSERT statement not working?

Hi folks, im just teaching myself php and mysql using the headfirst book but im a little bit stuck being a noob to this code. Im using phpmyadmin for the database and tables and ive succesfully connected to the database BUT the query is coming back error querying database..this is the php code ive got... <?php $dbc =' ', 'username', 'password', 'database name') or die('Error connecting to the 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 ('Sally', 'Jones', '3 days ago'. '1 day', 'four', " . "'green with six tentacles', 'We just talked and played with a dog', " . "'yes', 'I may have seen your dog. Contact me.', " . "'sally@gregs-list.net')" ; $result = mysqli_query($dbc, $query) or die('Error querying database.'); ?> Can anyone help me? :) Cheers. John i use the mysqli_connect before $dbc i deleted a bit too much info when i was posting the question. the error that comes back on the screen when submitting the form is simply "error querying database" so some part of the query is wrong. Just finding out which part of it is the tricky bit.

Public Comments

  1. This question is a bit too obscure to answer. It would help if you provided the structure of the `aliens_abduction` table, also the full error. P.S. I assume that it's a typo, in the first row. $dbc should be a valid connection. Do you use mysqli_connect? P.P.S. I will edit the answer periodically, after you provide more details.
  2. You query is messy with concatenating the string. There is no need for that. When I rewrote it like this: $query = "INSERT INTO aliens_abduction (first_name, last_name, When_it_happened, how_long, how_many, alien_description, hat_they_did, fang_spotted, other, email) VALUES ('Sally', 'Jones', '3 days ago'. '1 day', 'four', 'green with six tentacles', 'We just talked and played with a dog', 'yes', 'I may have seen your dog. Contact me.', 'sally@gregs-list.net')" ; I see you have a period instead of a comma between 3 dasy ago and 1 day: '3 days ago'. '1 day', Also, to help debug, instead of this: $result = mysqli_query($dbc, $query) or die('Error querying database.'); do this while testing (remove when code goes live) $result = mysqli_query($dbc, $query) if (mysql_error()){ echo $query .'<br />'; echo mysql_error(); } This will output the query and error message to help find the problem.
Powered by Yahoo! Answers