psyqosis Posted October 15, 2007 Share Posted October 15, 2007 Hi. I thought I understood this, but I keep getting an error. Can anyone help? Thanks. 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 'desc) VALUES ('test1', 'test2', 'test3')' at line" CODE (I'm using 3 form inputs to insert data via submit button): <?php $database = "edit" or die(mysql_error()); $db = mysql_connect("edit", "edit", "edit") or die(mysql_error()); mysql_select_db($database, $db) or die(mysql_error()); ?> <?php if($add == 1) { $sql = "INSERT INTO events (title, date, desc) VALUES ('$title', '$date', '$desc');"; mysql_query($sql, $db) or die(mysql_error()); //error comes from here ?> <h3>Record added to database. Thanks.</h3> Add another record by filling in the form below or return to the main menu by <a href="index.php">clicking here</a>. <?php } ?> <form method=post action="<?php echo $PHP_SELF ?>" enctype="multipart/form-data"> <h3> Create New Record </h3> <center> <table width=400 cellpadding=0 cellspacing=0 border=0> <tr><td align=right nowrap>Product Title: </td><td><input size=70 type="text" name="title"></td></tr> <tr><td align=right nowrap>Date: </td><td><input size=70 type="text" name="date"></td></tr> <tr><td align=right nowrap>Description: </td><td><input size=70 type="text" name="desc"></td></tr> <tr><td colspan=2 align=right><input type=hidden name="add" value="1"><input type="submit" value="Submit Record"></td></tr> </table> Link to comment https://forums.phpfreaks.com/topic/73325-solved-insert-syntax/ Share on other sites More sharing options...
mattal999 Posted October 15, 2007 Share Posted October 15, 2007 i think this: <?php if($add == 1) { $sql = "INSERT INTO events (title, date, desc) VALUES ('$title', '$date', '$desc');"; mysql_query($sql, $db) or die(mysql_error()); //error comes from here ?> should be: <?php if($add == 1) { $sql = "INSERT INTO events (title, date, desc) VALUES ('$title', '$date', '$desc')"; mysql_query($sql, $db) or die(mysql_error()); //error comes from here ?> Link to comment https://forums.phpfreaks.com/topic/73325-solved-insert-syntax/#findComment-369951 Share on other sites More sharing options...
psyqosis Posted October 15, 2007 Author Share Posted October 15, 2007 ok, got rid of the extra semicolon, but still the same error results. Link to comment https://forums.phpfreaks.com/topic/73325-solved-insert-syntax/#findComment-369955 Share on other sites More sharing options...
kenrbnsn Posted October 15, 2007 Share Posted October 15, 2007 The word "date" is a reserved word in MySql. If you use a reserved word as a table or column name, you need to enclose it in backticks ( ` ): <?php $sql = "INSERT INTO events (title, `date`, desc) VALUES ('$title', '$date', '$desc')"; $rs = mysql_query($sql) or die("Problem with the query: $sql<br>" . mysql_error()); ?> Ken Link to comment https://forums.phpfreaks.com/topic/73325-solved-insert-syntax/#findComment-369956 Share on other sites More sharing options...
ToonMariner Posted October 15, 2007 Share Posted October 15, 2007 I think desc is a reserved word in mysql one of teh reasons I always use the back tick in my queries - even if they are not 'nessesscary'... try this $sql = "INSERT INTO `events` (`title`, `date`, `desc`) VALUES ('$title', '$date', '$desc')"; and make sure that the data you are adding fits the expected values of the fields in your database. kenrbnsn - its date is it?? I would remember but as I always use backticks I never have this problem!! Link to comment https://forums.phpfreaks.com/topic/73325-solved-insert-syntax/#findComment-369958 Share on other sites More sharing options...
psyqosis Posted October 15, 2007 Author Share Posted October 15, 2007 that's it! I need to get in the habit of backticking. You have been a lot of help. Link to comment https://forums.phpfreaks.com/topic/73325-solved-insert-syntax/#findComment-369960 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.