Page 1 of 1
mysql error. HELP!
Posted: Thu Jul 19, 2007 9:57 pm
by m2babaey
Hi.
This code prompts an error. How can I fix it?
Code: Select all
mysql_query("INSERT INTO details ( date, time, where ,who, price, action ,comments ) VALUES('$date','$time','$where','$who','$price' ,'$action' ,'$comments' )")
or die(mysql_error());
Error:
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 'where ,who, price, action ,comments ) VALUES('07/14/2007','0940
Posted: Thu Jul 19, 2007 10:04 pm
by Benjamin
Code: Select all
mysql_query("INSERT INTO `details` (`date`, `time`, `where` ,`who`, `price`, `action` ,`comments`) VALUES('" . mysql_real_escape_string($date) . "','" . mysql_real_escape_string($time) . "','" . mysql_real_escape_string($where) . "','" . mysql_real_escape_string($who) . "','" . mysql_real_escape_string($price) . "' ,'" . mysql_real_escape_string($action) . "' ,'" . mysql_real_escape_string($comments) . "' )")
OR die(mysql_error());
Posted: Thu Jul 19, 2007 10:15 pm
by m2babaey
I solved it by this code:
Code: Select all
mysql_query("INSERT INTO 'details' ( 'date', 'time', 'where' ,'who', 'price', 'action' ,'comments' ) VALUES('$date','$time','$where','$who','$price' ,'$action' ,'$comments' )")
or die(mysql_error());
But I had another code already like below and it was ( and is ) working
Code: Select all
mysql_query("INSERT INTO articles (section, title, text ,username, name ) VALUES('$subject','$title','$article','$username','$name' )")
or die(mysql_error());

Posted: Thu Jul 19, 2007 10:38 pm
by Benjamin
What are you saying?
Posted: Thu Jul 19, 2007 11:32 pm
by m2babaey
sorry
DO you know what i learnt today?
that ` is diffrernt from '

Posted: Fri Jul 20, 2007 6:05 am
by Gente
m2babaey wrote:I solved it by this code:
Code: Select all
mysql_query("INSERT INTO 'details' ( 'date', 'time', 'where' ,'who', 'price', 'action' ,'comments' ) VALUES('$date','$time','$where','$who','$price' ,'$action' ,'$comments' )")
or die(mysql_error());
But I had another code already like below and it was ( and is ) working
Code: Select all
mysql_query("INSERT INTO articles (section, title, text ,username, name ) VALUES('$subject','$title','$article','$username','$name' )")
or die(mysql_error());

First fragment consist reserved MySQL words ('where' for example) which you should put into `...` to avoid the error
Posted: Fri Jul 20, 2007 4:31 pm
by califdon
Gente wrote:First fragment consist reserved MySQL words ('where' for example) which you should put into `...` to avoid the error
Better yet,
don't use reserved words as names for variables or fields!