Error in your SQL syntax?

Hiii

can someone tell me why this code produces this error?

[php]

$one = “one”;
$two = “two”;
$three = “three”;

$query = “INSERT INTO to_do_db.to_do_table (description, flags, status) VALUES ($one , $two , $three)”;

mysql_query($query) or die (mysql_error());[/php]

“You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘a test entry , Normal , Normal)’ at line 1”

I copied that INSERT INTO style from the way PHPMyadmin did it, even without the to_do_db. in there it still doesn’t work. tried every combination of punctuation…

thanks

you must select db with mysql_select_db() function
and after write:
$query = “INSERT INTO to_do_table (description, flags, status) VALUES (’$one’ , ‘$two’ , ‘$three’)”;

Sorry, didn’t post the entire script. but yeah, I used that… I can create database, add tables and such but so far can’t get any data into them. here’s the entire script:

[php]

<?php $sql_host = "localhost"; $sql_username = "root"; $sql_password = ""; $sql_database = "to_do_db"; mysql_connect($sql_host, $sql_username, $sql_password) or die (mysql_error()); mysql_select_db($sql_database) or die (mysql_error()); $one = mysql_real_escape_string($_POST['description']); $two = mysql_real_escape_string($_POST['status']); $three = mysql_real_escape_string($_POST['flags']); echo $one . "
"; echo $two . "
"; echo $three . "
"; $query = "INSERT INTO `to_do_db`.`to_do_table` (`description`, `flags`, `status`) VALUES ($one , $two , $three)"; mysql_query($query) or die (mysql_error()); mysql_close(); ?>

Home

[/php]

Remove to_do_db from sentence
You write :
INSERT INTO to_do_db.to_do_table

and you must write :
INSERT INTO to_do_table

as i said:

Remove to_do_db from sentence
Instead of :to_do_db.to_do_table

INSERT into again to_do_table

It is perfectly fine to qualify a table name with the database name that contains the table. That is not the problem. The problem is that there are no quotes around the string values in the query. It should be:

[php]
$query = “INSERT INTO to_do_db.to_do_table (description, flags, status)
VALUES (’$one’ , ‘$two’ , ‘$three’)”;
[/php]

Note: I don’t use backticks. There are only needed if the field name is a reserved word.

Sponsor our Newsletter | Privacy Policy | Terms of Service