sabo Posted February 12, 2009 Share Posted February 12, 2009 I wrote a small web form and it does not seem to transfer data to the php script. Here is the html form <form method="post" action="insert.php"> <BR> <BR> <BR> <center> <table> <tr> Enter your weight Watcher Food to keep track of your points. <td>Enter the Food:</td> <td><input type="text" name="food"></td> </tr> <BR> <tr> Enter the Points for the food you ate. <td>Points:</td> <td><input type="text" name="points"></td> </tr> <tr> <td><input type="submit" name="submit" value="Enter"></td> </tr> </center> </form> this php script works and enters the data into the databse when executed on the command line #php insert.php # cat insert.php <?php $food = $_POST['food']; $points = $_POST['points']; // Make a MySQL Connection $con = mysql_connect("localhost", "root", "pass"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("watchers", $con); // Insert a row of information into the table "example" #$sql="INSERT INTO Meal (food, points) VALUES ('$_POST[food]','$_POST[points]')"; $sql="INSERT INTO Meal (food, points) VALUES ('$food','$points')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added"; mysql_close($con) ?> Any Ideas? Thanks for reading Quote Link to comment https://forums.phpfreaks.com/topic/144885-web-form-will-not-write-to-database/ Share on other sites More sharing options...
9three Posted February 12, 2009 Share Posted February 12, 2009 You are not sending a query to your database through PHP. You are only assigning a value to your variable $sql, and checking if mysql_query failed. Quote Link to comment https://forums.phpfreaks.com/topic/144885-web-form-will-not-write-to-database/#findComment-760325 Share on other sites More sharing options...
PFMaBiSmAd Posted February 12, 2009 Share Posted February 12, 2009 What output from your code are you getting and are you developing and debugging this on a system with error_reporting set to E_ALL and display_errors set to ON so that you immediately see any php generated errors? Quote Link to comment https://forums.phpfreaks.com/topic/144885-web-form-will-not-write-to-database/#findComment-760328 Share on other sites More sharing options...
sabo Posted February 12, 2009 Author Share Posted February 12, 2009 Thanks for the tips. PFMaBiSmAd and 9three I have enabled display_errors. It was set to off E_ALL was already enabled in /etc/php.ini 9three Could you elaborate on your comment. This is my first mysql php script. Can you mention the change that will send the DB my query. thanks Quote Link to comment https://forums.phpfreaks.com/topic/144885-web-form-will-not-write-to-database/#findComment-760583 Share on other sites More sharing options...
premiso Posted February 12, 2009 Share Posted February 12, 2009 A few changes: # cat insert.php <?php $food = $_POST['food']; $points = $_POST['points']; // Make a MySQL Connection $con = mysql_connect("localhost", "root", "pass") or die('Could not connect: ' . mysql_error()); mysql_select_db("watchers", $con); // Insert a row of information into the table "example" #$sql="INSERT INTO Meal (food, points) VALUES ('$_POST[food]','$_POST[points]')"; $sql="INSERT INTO Meal (food, points) VALUES ('$food','$points')"; // second param is not necessary mysql_query($sql) or die(die('SQL Was: ' . $sql . "\nError: " . mysql_error()); echo "1 record added"; // not needed mysql_close($con); ?> See what that does for you. Quote Link to comment https://forums.phpfreaks.com/topic/144885-web-form-will-not-write-to-database/#findComment-760591 Share on other sites More sharing options...
sabo Posted February 12, 2009 Author Share Posted February 12, 2009 Yes That did it. Thanks for the help. Now that it works I can figure out what I was doing wrong. Quote Link to comment https://forums.phpfreaks.com/topic/144885-web-form-will-not-write-to-database/#findComment-760608 Share on other sites More sharing options...
premiso Posted February 12, 2009 Share Posted February 12, 2009 Yes That did it. Thanks for the help. Now that it works I can figure out what I was doing wrong. You were missing a ; after mysql_close in the original code. Since I removed that line it took out that error. Quote Link to comment https://forums.phpfreaks.com/topic/144885-web-form-will-not-write-to-database/#findComment-760613 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.