madjack87 Posted January 31, 2009 Share Posted January 31, 2009 I have a test.php page and a insert.php page on my site Here is the code for the insert.php <?php $connection = mysql_connect("mysql","madjack87","polosport1"); if (!$connection){ die("Could not connect: ") . mysql_error()); } mysql_select_db("madjacknews", $connection); $sql="INSERT INTO members (email) VALUES ('$_POST[email]')"; if(!mysql_query($sql,$connection)){ die("Error: " . mysql_error()); } echo "1 record added"; mysql_close($connection) ?> Here is the code for the test.php page <form action="insert.php" method="post"> Email: <input type="text" name="email" /> <input type="submit" /> </form> After clicking submit on the form it takes me to insert.php and the page is blank. And when checking phpmyadmin there where no new records added. Any help? Quote Link to comment https://forums.phpfreaks.com/topic/143306-inserting-from-form-to-databasei-need-help-please/ Share on other sites More sharing options...
Snart Posted January 31, 2009 Share Posted January 31, 2009 $_POST doesn't exist. $_POST['email'] or $_POST["email"] do. Quote Link to comment https://forums.phpfreaks.com/topic/143306-inserting-from-form-to-databasei-need-help-please/#findComment-751577 Share on other sites More sharing options...
madjack87 Posted January 31, 2009 Author Share Posted January 31, 2009 Thanks Check this out http://www.w3schools.com/php/php_mysql_insert.asp They have the wrong syntax then! Quote Link to comment https://forums.phpfreaks.com/topic/143306-inserting-from-form-to-databasei-need-help-please/#findComment-751578 Share on other sites More sharing options...
madjack87 Posted January 31, 2009 Author Share Posted January 31, 2009 I changed what you said needed to be changed.. and now it reloads the test.php page but doesnt seem to add any records. madjacknews.com/test.php is the page I am talking about. Quote Link to comment https://forums.phpfreaks.com/topic/143306-inserting-from-form-to-databasei-need-help-please/#findComment-751582 Share on other sites More sharing options...
JewGold Posted January 31, 2009 Share Posted January 31, 2009 did you escape the quotes? $sql="INSERT INTO members (email) VALUES ('$_POST[\"email\"]')"; you should mysql_real_escape_string the $_POST["email"] before running it through a query. Quote Link to comment https://forums.phpfreaks.com/topic/143306-inserting-from-form-to-databasei-need-help-please/#findComment-751588 Share on other sites More sharing options...
Snart Posted January 31, 2009 Share Posted January 31, 2009 Could you place an echo of $sql after you've built up the query and before you execute it? Quote Link to comment https://forums.phpfreaks.com/topic/143306-inserting-from-form-to-databasei-need-help-please/#findComment-751589 Share on other sites More sharing options...
madjack87 Posted January 31, 2009 Author Share Posted January 31, 2009 Yea I escaped the quotes, Im still supmped Quote Link to comment https://forums.phpfreaks.com/topic/143306-inserting-from-form-to-databasei-need-help-please/#findComment-751592 Share on other sites More sharing options...
premiso Posted January 31, 2009 Share Posted January 31, 2009 You do not need mysql_close as an fyi. <?php $connection = mysql_connect("mysql","madjack87","polosport1"); if (!$connection){ die("Could not connect: ") . mysql_error()); } mysql_select_db("madjacknews") or die("Unable to select database: " . mysql_error()); $sql="INSERT INTO members (email) VALUES ('{$_POST['email']}')"; // notice the use of { and }, doing that will allow the array index to display without error. if(!is_resource(mysql_query($sql))){ die("Error: " . mysql_error()); } echo "1 record added"; ?> Also the $connection is not required for mysql unless you are using multiple databases. Try the above and see what happens...I also added to your if the is_resource definition. If the SQL had a syntax error it will not return a valid resource, so that is a decent check to do. Quote Link to comment https://forums.phpfreaks.com/topic/143306-inserting-from-form-to-databasei-need-help-please/#findComment-751593 Share on other sites More sharing options...
madjack87 Posted January 31, 2009 Author Share Posted January 31, 2009 I just tried this exact code still no change. You do not need mysql_close as an fyi. <?php $connection = mysql_connect("mysql","madjack87","polosport1"); if (!$connection){ die("Could not connect: ") . mysql_error()); } mysql_select_db("madjacknews") or die("Unable to select database: " . mysql_error()); $sql="INSERT INTO members (email) VALUES ('{$_POST['email']}')"; // notice the use of { and }, doing that will allow the array index to display without error. if(!is_resource(mysql_query($sql))){ die("Error: " . mysql_error()); } echo "1 record added"; ?> Also the $connection is not required for mysql unless you are using multiple databases. Try the above and see what happens...I also added to your if the is_resource definition. If the SQL had a syntax error it will not return a valid resource, so that is a decent check to do. Quote Link to comment https://forums.phpfreaks.com/topic/143306-inserting-from-form-to-databasei-need-help-please/#findComment-751596 Share on other sites More sharing options...
madjack87 Posted January 31, 2009 Author Share Posted January 31, 2009 Im new to PHP so where exactly in the code would I place an echo? Could you place an echo of $sql after you've built up the query and before you execute it? Quote Link to comment https://forums.phpfreaks.com/topic/143306-inserting-from-form-to-databasei-need-help-please/#findComment-751597 Share on other sites More sharing options...
Snart Posted January 31, 2009 Share Posted January 31, 2009 Right after you've built up $sql: $sql="INSERT INTO members (email) VALUES ('{$_POST['email']}')"; // notice the use of { and }, doing that will allow the array index to display without error. echo $sql; Quote Link to comment https://forums.phpfreaks.com/topic/143306-inserting-from-form-to-databasei-need-help-please/#findComment-751602 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.