Time Traveller Posted January 24, 2007 Share Posted January 24, 2007 Hi folks, Im a newb to both php and mysql. Im working through a tutorial and having a problem of inserting data into a table. I have installed xampp and am using the standard windows install with no modifications. In mysql I have created a new database with phpmyadmin and setup a table with the following fields: id int(6) auto_increment name VARCHAR(20) addr VARCHAR(100) desc VARCHAR(200)Here is the codeindex.php[code]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head> <title></title> <link rel="stylesheet" href="index.css" type="text/css"></head><body> <div id="wrap"> <div id="pageHead"> <?php echo date("D M j G:i:s T Y"); ?> </div> <div id="pageInfo"> Website Links </div> <div id="linkAddr"><b>Name</b></div> <div id="linkDesc"><b>Description</b> </div> <div id="linkDel"></div> <br id="fClear" /> <br /> <div id="AddNewForm"> <form action=form.php method=post> <table> <tr> <td>Name</td><td><input type="text" name="name" size="20" /></td> </tr> <tr> <td>Address</td><td><input type="text" name="addr" size="80" /></td> </tr> <tr> <td>Description</td><td><input type="text" name="desc" size="80" /></td> </tr> <tr> <td><input type="submit" value="Add Link" name="addNew" /></td> </tr> </table> </form> </div> </div></body></html>[/code]And form.php[code]<? echo "Starting form.php script.<br />"; $link = mysql_connect('localhost', 'root', 'rootpass') or die("Could not connect: " . mysql_error()); echo "Connected to mysql.<br />"; @mysql_select_db('linkdb') or die("Could not select database"); echo "Database selected.<br />"; if($_POST['name']!=""){ $linkN=htmlentities($_POST['name']); $linkA=htmlentities($_POST['addr']); $linkD=htmlentities($_POST['desc']); mysql_query("INSERT INTO links (name, addr, desc) VALUES('$linkN','$linkA','$linkD');"); echo "Link: $linkN inserted.<br />"; } mysql_close($link); echo "Connection closed.<br />";?>[/code]When I submit a new record, form.php echos every message yet when I browse the rows in phpmyadmin there is nothing other than those which I entered through phpmyadmin. Can anyone point out what I'm doing wrong? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/35526-solved-newb-insert-trouble/ Share on other sites More sharing options...
fenway Posted January 24, 2007 Share Posted January 24, 2007 Check mysql_error() after the insert call.... Quote Link to comment https://forums.phpfreaks.com/topic/35526-solved-newb-insert-trouble/#findComment-168483 Share on other sites More sharing options...
Time Traveller Posted January 26, 2007 Author Share Posted January 26, 2007 Thanks, I added this line to form.php at line #13 [code]echo mysql_errno($link) . ": " . mysql_error($link). "<br />";[/code]When I submit the form, I get the following error:[quote]1064: 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('spam','eggs','ham')' at line 1[/quote]I had tried putting all field names in quotes, have looked at mysql_query, and have compared my code to code that works on this setup. I dont see what the syntax error could be. Quote Link to comment https://forums.phpfreaks.com/topic/35526-solved-newb-insert-trouble/#findComment-169637 Share on other sites More sharing options...
fenway Posted January 26, 2007 Share Posted January 26, 2007 DESC is a reserved keyword -- change it to description. Quote Link to comment https://forums.phpfreaks.com/topic/35526-solved-newb-insert-trouble/#findComment-169645 Share on other sites More sharing options...
Time Traveller Posted January 26, 2007 Author Share Posted January 26, 2007 Thanks again, Fenway. I now see the new records showing in PMA. Quote Link to comment https://forums.phpfreaks.com/topic/35526-solved-newb-insert-trouble/#findComment-170025 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.