Page 1 of 1

INSERT data into database

Posted: Sat Mar 31, 2012 11:10 pm
by miraklose
Hi guys, i've been trying to insert data into my database but i keep getting this 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 'procedure,date) VALUES ('tag1','new post','01/04/12 02:55:34')' at line 1
Below is my code. I can't figure out where it went wrong.. can someone point it out to me please? thanks..

Code: Select all

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// get data that sent from form 
$tags=$_POST['tags'];
$procedure=$_POST['tulis'];
$datetime=date("d/m/y h:i:s"); //create date time

$sql="INSERT INTO $tbl_name(tags,procedure,date) VALUES ('$tags','$procedure','$datetime')";
$result=mysql_query($sql);

if($result){
echo "Successful<BR>";
}
else {
echo "Error: " . mysql_error()."<br>";
}
mysql_close();

Re: INSERT data into database

Posted: Sun Apr 01, 2012 2:53 am
by social_experiment
"procedure" is a reserved word in SQL; use a different column name or try and enclose the column names in backtics `colName` (this should standard practise)

Re: INSERT data into database

Posted: Sun Apr 01, 2012 11:53 am
by califdon
Reserved words should not be used for field names or table names, as good practice, but you can overcome this by using backticks, as social_experiment said. A list of MySQL reserved words can be found at http://dev.mysql.com/doc/refman/5.5/en/ ... words.html