INSERT data into database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
miraklose
Forum Newbie
Posts: 2
Joined: Sat Mar 31, 2012 10:39 pm

INSERT data into database

Post 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();
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: INSERT data into database

Post 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)
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: INSERT data into database

Post 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
Post Reply