phpfreaksftsolitude Posted May 4, 2007 Share Posted May 4, 2007 ==================== MY CODE. I'm trying to insert a record into a table in my database. It doesn't really get any simpler than this! But for some reason it's not working no matter what I try, I get the error message below. ==================== $sql = "INSERT INTO phonebook (name) VALUES ('Dave')"; if (mysql_query($sql)) { echo("Your record has been added."); } else { echo("Error adding submitted record: " . mysql_error()); } ==================== ERROR MESSAGE (below). I feel like I've checked the syntax a hundred times. Can somebody help me? Thanks! ==================== Error adding submitted record: 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 'character (name) VALUES ('Dave')' at line 1 Quote Link to comment Share on other sites More sharing options...
trq Posted May 4, 2007 Share Posted May 4, 2007 Put simply... there is nothing wrong with that code. Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted May 4, 2007 Share Posted May 4, 2007 Try this: $sql = "INSERT INTO phonebook (name) VALUES ('Dave')"; $result=mysql_query($sql); if ($result) { echo("Your record has been added."); } else { echo("Error adding submitted record: " . mysql_error()); } Quote Link to comment Share on other sites More sharing options...
tauchai83 Posted May 4, 2007 Share Posted May 4, 2007 I din see anything wrong. But you may try the one posted by mmarif4u 1st. Quote Link to comment Share on other sites More sharing options...
trq Posted May 4, 2007 Share Posted May 4, 2007 Try this: $sql = "INSERT INTO phonebook (name) VALUES ('Dave')"; $result=mysql_query($sql); if ($result) { echo("Your record has been added."); } else { echo("Error adding submitted record: " . mysql_error()); } That is exactly the same. Quote Link to comment Share on other sites More sharing options...
phpfreaksftsolitude Posted May 4, 2007 Author Share Posted May 4, 2007 Thanks guys, but none of those worked. Could it have anything to do with the fact that I'm using Yahoo? Or maybe the auto increment for the "id" field isn't working like it should? I dunno, I'm am totally out of ideas.... Quote Link to comment Share on other sites More sharing options...
phpfreaksftsolitude Posted May 4, 2007 Author Share Posted May 4, 2007 I'm close to figuring it out!!! Okay, it DOES have something to do with my database table. I created a new empty table, didn't do anything fancy to it, and the insert function worked fine! (I can't believe I spent HOURS on this, dang). So I think the problem with my existing table is in the "auto increment" feature. In the table, for the "id" row, I have set the following: Type = int Attributes = UNSIGNED Extra = auto_increment What else do I need to make this auto increment feature work correctly? Thanks in advance.... Quote Link to comment Share on other sites More sharing options...
StarBuck Posted May 4, 2007 Share Posted May 4, 2007 normally INSERT queries are like "INSERT INTO table VALUES('','bob','smith')" double single quotes would be the ID you want to increment and bob would be first name field, smith last name field. Thats old school though you might be using a newer method. Quote Link to comment Share on other sites More sharing options...
SkyRanger Posted May 4, 2007 Share Posted May 4, 2007 Yeah, I had the same problem with it inserting and getting the same errors. best thing to do is do the "insert into table (id,name) VALUES ('', '$name')" If that doesn't work, do a manual insert through phpMyAdmin and check the code that is used and just apply it to your script. Quote Link to comment Share on other sites More sharing options...
neel_basu Posted May 4, 2007 Share Posted May 4, 2007 in auto_increment ed fields you doesn't need to enter any values it will get incremented automatically. Try this $result=mysql_query($sql) or die(mysql_error()); And make sure is the auto_incremented field Your PRIMARY KEY ??? ??? ?? Quote Link to comment Share on other sites More sharing options...
phpfreaksftsolitude Posted May 4, 2007 Author Share Posted May 4, 2007 It is set as my primary key (I am using phpMyAdmin to set up the database). But still no luck. It's strange, I can insert into to the new table (which has no primary key yet) but I still cannot insert into the existing table. Should I just delete the "id" field and add a new one to the table? Should I make another field my primary? Quote Link to comment Share on other sites More sharing options...
neel_basu Posted May 4, 2007 Share Posted May 4, 2007 You tried $result=mysql_query($sql) or die(mysql_error()); ?? Quote Link to comment Share on other sites More sharing options...
phpfreaksftsolitude Posted May 4, 2007 Author Share Posted May 4, 2007 Yes, I tried that. That's my whole problem, it works in for one table but not the other. Why would the same code work to insert a record into one table and not in another within the same database? Quote Link to comment Share on other sites More sharing options...
tauchai83 Posted May 5, 2007 Share Posted May 5, 2007 could you please post your DB design? all the fields...and possibly post the whole code doesn't work. Quote Link to comment Share on other sites More sharing options...
phpfreaksftsolitude Posted May 5, 2007 Author Share Posted May 5, 2007 I am going to close this issue because I ended up deleting the table and all its records (like I said, it was driving me crazy). So I recreated a new table (more carefully this time) and tested it along the way, and it works perfectly! No idea why the first table caused me so much pain. Now I've just gotta enter in my old records one by one, which isn't too big of a deal. Thanks for all your help, you guys helped me flesh out my query structure and that's what counts! -S 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.