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> Quote Link to comment 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 ?> Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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!! Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.