Page 1 of 1

mysql insert

Posted: Sun Jul 08, 2007 7:31 pm
by ayfine
Hey

I have no idea why this isn't working. The insert code is this

Code: Select all

$insert = mysql_query("INSERT into user_subs (title, desc, url, type, tags, date, author_id) values ('$title', '$final', '$url', 'story', '$tags', '$date', {$_SESSION['uid']})") or die(mysql_error());
This is the error I am getting when I submit

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 'desc, url, type, tags, date, author_id) values ('awesome', 'asdffff', 'http://ch' at line 1

Any idea whats wrong?

Posted: Sun Jul 08, 2007 7:32 pm
by volka
Do you get an error message?
Please post the error message.

Posted: Sun Jul 08, 2007 7:33 pm
by ayfine
volka wrote:Do you get an error message?
Please post the error message.
Sorry

just edited it in.

Posted: Sun Jul 08, 2007 7:35 pm
by volka
desc is a reserved word for mysql
http://dev.mysql.com/doc/refman/5.0/en/ ... words.html lists all(?) reserved words and tells you how you can use them as field identifiers (if you must).

Posted: Sun Jul 08, 2007 7:37 pm
by ayfine
volka wrote:desc is a reserved word for mysql
http://dev.mysql.com/doc/refman/5.0/en/ ... words.html lists all(?) reserved words and tells you how you can use them as field identifiers (if you must).
Thank you!

Added an r at the end and now everything's fine!

Posted: Mon Jul 09, 2007 6:02 am
by designerman
To get a good error message out of mysql its a good idea to use the mysql_error(); function eg

Code: Select all

$sql = "INSERT INTO table (colum1, colum2, colum3) VALUES ('foo', 'bar', 'foo')"; 
if(!mysql_query($sql)){
  echo 'Error in query! Mysql said: ". mysql_error();
  echo '<br />The query: '. $sql;
}
Using a class for running mysql queries will offer these features by default.