Gem Posted March 5, 2009 Share Posted March 5, 2009 Hi all, I'm new to this forum so be nice lol I'm also very new to the whole MySQL and PHP thing, and its only taken me 3 days to connect to my webserver, so things are looking good. Anyway, I'm finally in ... and I'm experimenting with INSERT. I got this code from a tutorial, added my details and kind of hoped it would work, but it doesn't. The connection is fine, it's not that ... The DB is called "bssql", the table is called "name" and has 2 columns, "firstname" and "lastname" CHAR(20). I'm using the following form: <form action="insert.php" method="post"> Firstname: <input type="text" name="firstname" /> Lastname: <input type="text" name="lastname" /> <input type="submit" /> and the following for "insert.php" <?php $con = mysql_connect("host","user","password"); if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("bssql", $con);$sql="INSERT INTO name (firstname, lastname,) VALUES ('$_POST[firstname]','$_POST[lastname]',)";if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added";mysql_close($con) ?> and I get this responce ... "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 ') VALUES ('Roger','Wilson',)' at line 1" Roger Wilson is the name I was trying to insert into the table. I hope this is enough information for some to help me ... I'd really appreciate it if someone could point out where i'm going wrong please ... Many Thanks, and Kind Regards Gem Quote Link to comment https://forums.phpfreaks.com/topic/148014-solved-insert-into-help-please/ Share on other sites More sharing options...
rs_nayr Posted March 5, 2009 Share Posted March 5, 2009 Hi all, I'm new to this forum so be nice lol I'm also very new to the whole MySQL and PHP thing, and its only taken me 3 days to connect to my webserver, so things are looking good. Anyway, I'm finally in ... and I'm experimenting with INSERT. I got this code from a tutorial, added my details and kind of hoped it would work, but it doesn't. The connection is fine, it's not that ... The DB is called "bssql", the table is called "name" and has 2 columns, "firstname" and "lastname" CHAR(20). I'm using the following form: <form action="insert.php" method="post"> Firstname: <input type="text" name="firstname" /> Lastname: <input type="text" name="lastname" /> <input type="submit" /> and the following for "insert.php" <?php $con = mysql_connect("host","user","password"); if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("bssql", $con);$sql="INSERT INTO name (firstname, lastname,) VALUES ('$_POST[firstname]','$_POST[lastname]',)";if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added";mysql_close($con) ?> and I get this responce ... "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 ') VALUES ('Roger','Wilson',)' at line 1" Roger Wilson is the name I was trying to insert into the table. I hope this is enough information for some to help me ... I'd really appreciate it if someone could point out where i'm going wrong please ... Many Thanks, and Kind Regards Gem put a space in between your values values ("value1", "value2", "value3",); not ("value1","value2","value3"); Quote Link to comment https://forums.phpfreaks.com/topic/148014-solved-insert-into-help-please/#findComment-776889 Share on other sites More sharing options...
sasa Posted March 5, 2009 Share Posted March 5, 2009 change $sql="INSERT INTO name (firstname, lastname,) VALUES ('$_POST[firstname]','$_POST[lastname]',)"; to $sql="INSERT INTO name (firstname, lastname) VALUES ('$_POST[firstname]','$_POST[lastname]')"; you have extra commas Quote Link to comment https://forums.phpfreaks.com/topic/148014-solved-insert-into-help-please/#findComment-776890 Share on other sites More sharing options...
Gem Posted March 5, 2009 Author Share Posted March 5, 2009 OK - They both made sense, but unfortunatly that didn't do the trick ... I got all excited and everything then LOL hmmm... any other idea's? Thanks for your help, most appreciated x Quote Link to comment https://forums.phpfreaks.com/topic/148014-solved-insert-into-help-please/#findComment-776900 Share on other sites More sharing options...
sasa Posted March 5, 2009 Share Posted March 5, 2009 what is error now? Quote Link to comment https://forums.phpfreaks.com/topic/148014-solved-insert-into-help-please/#findComment-777029 Share on other sites More sharing options...
Gem Posted March 5, 2009 Author Share Posted March 5, 2009 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 ') VALUES ('roger', 'wilson')' at line 1 Same error but with a space between the 2 values ... Is it possible there is a setting in the table on the server or the database that isnt right or something?? x Quote Link to comment https://forums.phpfreaks.com/topic/148014-solved-insert-into-help-please/#findComment-777367 Share on other sites More sharing options...
kickstart Posted March 5, 2009 Share Posted March 5, 2009 Hi Change the line:- die('Error: ' . mysql_error()); to:- die('Error: ' . " $sql " . mysql_error()); and that should display the full sql. Hopefully we can then see the problem. Sql generally should not care about using a space after the comma. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/148014-solved-insert-into-help-please/#findComment-777376 Share on other sites More sharing options...
Gem Posted March 5, 2009 Author Share Posted March 5, 2009 Error: INSERT INTO name (firstname, lastname,) VALUES ('roger', 'wilson') 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 ') VALUES ('roger', 'wilson')' at line 1 New error message ... Still doesnt mean anything to me though Quote Link to comment https://forums.phpfreaks.com/topic/148014-solved-insert-into-help-please/#findComment-777380 Share on other sites More sharing options...
revraz Posted March 5, 2009 Share Posted March 5, 2009 Remove the COMMA after lastname INSERT INTO name (firstname, lastname) VALUES ('roger', 'wilson') Quote Link to comment https://forums.phpfreaks.com/topic/148014-solved-insert-into-help-please/#findComment-777384 Share on other sites More sharing options...
kickstart Posted March 5, 2009 Share Posted March 5, 2009 Hi You still have an extra comma in there (after lastname). All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/148014-solved-insert-into-help-please/#findComment-777385 Share on other sites More sharing options...
Gem Posted March 5, 2009 Author Share Posted March 5, 2009 <?php $con = mysql_connect("host","user","pw"); if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("bssql", $con);$sql="INSERT INTO name (firstname, lastname,) VALUES ('$_POST[firstname]', '$_POST[lastname])'";if (!mysql_query($sql,$con)) { die('Error: ' . " $sql " . mysql_error()); } echo "1 record added";mysql_close($con) ?> Hey Keith - thanks for your help mate ... Which comma is it that shouldnt be there? Please... XXX Quote Link to comment https://forums.phpfreaks.com/topic/148014-solved-insert-into-help-please/#findComment-777388 Share on other sites More sharing options...
kickstart Posted March 5, 2009 Share Posted March 5, 2009 Hi The one I have highlighted in red INSERT INTO name (firstname, lastname,) VALUES All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/148014-solved-insert-into-help-please/#findComment-777392 Share on other sites More sharing options...
Gem Posted March 5, 2009 Author Share Posted March 5, 2009 Thanks mate - thats done it! Now to try making information go into 2 tables ... fun! Who'd of thought one little comma would cause 2 days worth of headaches!LOL Thanks again xxx Quote Link to comment https://forums.phpfreaks.com/topic/148014-solved-insert-into-help-please/#findComment-777406 Share on other sites More sharing options...
kickstart Posted March 5, 2009 Share Posted March 5, 2009 Hi No problem. Can I suggest that you format your code a bit more. Doesn't really make any difference to how it runs, but does make it easier for you to debug it. For example, put each statement on a new line and indent inside if statements, loops, etc. Eg:- <?php $con = mysql_connect("host","user","password"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("bssql", $con); $sql="INSERT INTO name (firstname, lastname) VALUES ('$_POST[firstname]','$_POST[lastname]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added"; mysql_close($con); ?> (although everyone has their own style of formatting, keeping it fairly consistant for yourself makes life easier). All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/148014-solved-insert-into-help-please/#findComment-777413 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.