Sobbs Posted March 20, 2009 Share Posted March 20, 2009 $con = mysql_connect("xxxxxxxxxxx","xxxxxxxx","xxxxxxxxxxx"); if (!$con) { die('Could not connect: ' . mysql_error()); }else{ echo "Connected to DB"; } $sql = "INSERT INTO `b33_3176976_lunadb`.`DB_Guides` (`gid`, `uname`, `gname`, `icon`, `desc`, `cat`, `body`, `score`) VALUES (NULL, \'$_POST['name']\', \'$_POST['guidename']\', \'$_POST['icon']\', \'$_POST['description']\', \'$_POST['cat']\', \'$_POST['body']\', \'0\');"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added"; mysql_close($con); This is supposed to add the info requested from a form but for somereason non of the info ever gets put into the database. The code doesnt even seem to work since it should say I was connected to the database but I always get a blank screen. Whats going wrong? Quote Link to comment https://forums.phpfreaks.com/topic/150386-sql-problems/ Share on other sites More sharing options...
revraz Posted March 20, 2009 Share Posted March 20, 2009 Blank screens usually indicate a syntax error. Turn on error display and error reporting and see if any errors are returned. Quote Link to comment https://forums.phpfreaks.com/topic/150386-sql-problems/#findComment-789781 Share on other sites More sharing options...
Sobbs Posted March 20, 2009 Author Share Posted March 20, 2009 How would I turn it on? Quote Link to comment https://forums.phpfreaks.com/topic/150386-sql-problems/#findComment-789788 Share on other sites More sharing options...
Maq Posted March 20, 2009 Share Posted March 20, 2009 You don't need to escape all of those single quotes in your values. Quote Link to comment https://forums.phpfreaks.com/topic/150386-sql-problems/#findComment-789802 Share on other sites More sharing options...
irkevin Posted March 20, 2009 Share Posted March 20, 2009 ive noticed an extra semicolumn after the insert query "INSERT INTO `b33_3176976_lunadb`.`DB_Guides` (`gid`, `uname`, `gname`, `icon`, `desc`, `cat`, `body`, `score`) VALUES (NULL, \'$_POST['name']\', \'$_POST['guidename']\', \'$_POST['icon']\', \'$_POST['description']\', \'$_POST['cat']\', \'$_POST['body']\', \'0\');"; try this $con = mysql_connect("xxxxxxxxxxx","xxxxxxxx","xxxxxxxxxxx"); if (!$con) { die('Could not connect: ' . mysql_error()); }else{ echo "Connected to DB"; } $sql = "INSERT INTO `b33_3176976_lunadb`.`DB_Guides` (`gid`, `uname`, `gname`, `icon`, `desc`, `cat`, `body`, `score`) VALUES (NULL, \'$_POST['name']\', \'$_POST['guidename']\', \'$_POST['icon']\', \'$_POST['description']\', \'$_POST['cat']\', \'$_POST['body']\', \'0\')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added"; mysql_close($con); Quote Link to comment https://forums.phpfreaks.com/topic/150386-sql-problems/#findComment-789803 Share on other sites More sharing options...
revraz Posted March 20, 2009 Share Posted March 20, 2009 That's not really a extra semi colon, its a SQL ending semi colon, which is just fine. ive noticed an extra semicolumn after the insert query Quote Link to comment https://forums.phpfreaks.com/topic/150386-sql-problems/#findComment-789804 Share on other sites More sharing options...
Sobbs Posted March 20, 2009 Author Share Posted March 20, 2009 Nope, no luck. Quote Link to comment https://forums.phpfreaks.com/topic/150386-sql-problems/#findComment-789808 Share on other sites More sharing options...
revraz Posted March 20, 2009 Share Posted March 20, 2009 With what? Did you enable error reporting? ini_set ("display_errors", "1"); error_reporting(E_ALL); Nope, no luck. Quote Link to comment https://forums.phpfreaks.com/topic/150386-sql-problems/#findComment-789810 Share on other sites More sharing options...
revraz Posted March 20, 2009 Share Posted March 20, 2009 Also, remove the gid and the NULL from your insert statement, you don't need it if it's set to auto increment. Quote Link to comment https://forums.phpfreaks.com/topic/150386-sql-problems/#findComment-789812 Share on other sites More sharing options...
Sobbs Posted March 20, 2009 Author Share Posted March 20, 2009 Im unable to enable error reporting for some reason. And it was in response to irkevin's attempt. Quote Link to comment https://forums.phpfreaks.com/topic/150386-sql-problems/#findComment-789814 Share on other sites More sharing options...
revraz Posted March 20, 2009 Share Posted March 20, 2009 Post the entire code. Quote Link to comment https://forums.phpfreaks.com/topic/150386-sql-problems/#findComment-789818 Share on other sites More sharing options...
Sobbs Posted March 20, 2009 Author Share Posted March 20, 2009 That pretty much is the entire code. There isn't much to it. Its simply ment to add info from a form into a database Quote Link to comment https://forums.phpfreaks.com/topic/150386-sql-problems/#findComment-789822 Share on other sites More sharing options...
irkevin Posted March 20, 2009 Share Posted March 20, 2009 well. i dont see mysql_select_db() in your code. do you have it or not? Quote Link to comment https://forums.phpfreaks.com/topic/150386-sql-problems/#findComment-789823 Share on other sites More sharing options...
Sobbs Posted March 20, 2009 Author Share Posted March 20, 2009 I added that and its still not enetering anything into the database. Ive re written the code without any post data. <?php ini_set ("display_errors", "1"); error_reporting(E_ALL); $con = mysql_connect("--","--","--"); if (!$con) { die('Could not connect: ' . mysql_error()); }else{ echo "Connected to DB"; } $db = mysql_select_db("b33_3176976_lunadb", $con); if (!$db) { die('Could not connect: ' . mysql_error()); }else{ echo "DB selected"; } mysql_query("INSERT INTO DB_Guide (uname, gname, icon, desc, cat, body, score) VALUES ('test', 'test', 'test', 'test', 'test', '0')"); ?> It still doesn apear to work. Quote Link to comment https://forums.phpfreaks.com/topic/150386-sql-problems/#findComment-789910 Share on other sites More sharing options...
trq Posted March 20, 2009 Share Posted March 20, 2009 desc is a reserved word, either change the field identifier (recommended) or surround it with `backticks`. if (!mysql_query("INSERT INTO DB_Guide (uname, gname, icon, `desc`, cat, body, score) VALUES ('test', 'test', 'test', 'test', 'test', '0')")) { echo mysql_error(); } Quote Link to comment https://forums.phpfreaks.com/topic/150386-sql-problems/#findComment-789914 Share on other sites More sharing options...
Sobbs Posted March 21, 2009 Author Share Posted March 21, 2009 Its still not working. if (!mysql_query("INSERT INTO DB_Guide (uname, gname, icon, dsc, cat, body, score) VALUES ('test', 'test', 'test', '1', 'test', '0')")) { echo mysql_error(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/150386-sql-problems/#findComment-790016 Share on other sites More sharing options...
trq Posted March 21, 2009 Share Posted March 21, 2009 What does mysql_error() produce? Quote Link to comment https://forums.phpfreaks.com/topic/150386-sql-problems/#findComment-790019 Share on other sites More sharing options...
Sobbs Posted March 21, 2009 Author Share Posted March 21, 2009 Im not getting any errors showing up. It apears to be running properly, but nothing is added into the database. Quote Link to comment https://forums.phpfreaks.com/topic/150386-sql-problems/#findComment-790024 Share on other sites More sharing options...
trq Posted March 21, 2009 Share Posted March 21, 2009 That makes little sense. Try this.... if (!mysql_query("INSERT INTO DB_Guide (uname, gname, icon, dsc, cat, body, score) VALUES ('test', 'test', 'test', '1', 'test', '0')")) { echo mysql_error(); } else { echo "success"; } Do you get any output? Quote Link to comment https://forums.phpfreaks.com/topic/150386-sql-problems/#findComment-790025 Share on other sites More sharing options...
Ninjakreborn Posted March 21, 2009 Share Posted March 21, 2009 Values != fieldnames. uname, gname, icon, dsc, cat, body, score (7) test, test, test, 1, test, 0 (6) You need as many values as you need fields. Quote Link to comment https://forums.phpfreaks.com/topic/150386-sql-problems/#findComment-790026 Share on other sites More sharing options...
Sobbs Posted March 21, 2009 Author Share Posted March 21, 2009 That makes little sense. Try this.... if (!mysql_query("INSERT INTO DB_Guide (uname, gname, icon, dsc, cat, body, score) VALUES ('test', 'test', 'test', '1', 'test', '0')")) { echo mysql_error(); } else { echo "success"; } Do you get any output? None. if (!mysql_query("INSERT INTO DB_Guide (uname, gname, icon, dsc, cat, body, score) VALUES ('test', 'test', 'test', 'test', '1', 'test', '0')")) { echo mysql_error(); } else { echo "success"; } Quote Link to comment https://forums.phpfreaks.com/topic/150386-sql-problems/#findComment-790028 Share on other sites More sharing options...
Ninjakreborn Posted March 21, 2009 Share Posted March 21, 2009 I have just told you what one of the problems where. The number of fields you have does not equal the number of values you are trying to put in. You listed 7 fields and you are only filling out 6 values. It will not allow that to go through. You need to put down one more value to match the number of fields you are entering or drop off one of the fields (either one). Quote Link to comment https://forums.phpfreaks.com/topic/150386-sql-problems/#findComment-790030 Share on other sites More sharing options...
Sobbs Posted March 21, 2009 Author Share Posted March 21, 2009 I have just told you what one of the problems where. The number of fields you have does not equal the number of values you are trying to put in. You listed 7 fields and you are only filling out 6 values. It will not allow that to go through. You need to put down one more value to match the number of fields you are entering or drop off one of the fields (either one). If you looked at my updated code, I did change it. Quote Link to comment https://forums.phpfreaks.com/topic/150386-sql-problems/#findComment-790031 Share on other sites More sharing options...
Ninjakreborn Posted March 21, 2009 Share Posted March 21, 2009 It sounds like your server/ini have error reporting and outputing disabled. Use the following url: http://us.php.net/error_reporting and cut on the error reporting temporarily (at the top of your code). Afterwards verify the Database name in your code is the same as your database name on the server. Also your connection information...make sure it's the same as your web host. Not all hosts allow you to connect using "localhost" what webhost are you using right now? Quote Link to comment https://forums.phpfreaks.com/topic/150386-sql-problems/#findComment-790034 Share on other sites More sharing options...
Maq Posted March 21, 2009 Share Posted March 21, 2009 There's something before the SQL block causing a blank page. Put this at the top of your script: ini_set ("display_errors", "1"); error_reporting(E_ALL); Quote Link to comment https://forums.phpfreaks.com/topic/150386-sql-problems/#findComment-790035 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.