Skelly123 Posted August 11, 2010 Share Posted August 11, 2010 Hi there everyone, I'm a first time poster, and I'm here because I'm, well, desperate. I'm just learning MySQL and PhP - I'm doing it online through tutorials, though I'd rather learn it in a classroom. What I am trying to do is send form data from a basic html form to a MySQL database. Super simple (so I thought), but what is happening is that when I fill out the form and hit "Submit" button, the echo text appears saying "1 record added," but then then when I go to check the database using phpmyadmin, the info I entered into the text boxes on the form does not appear there. I set up a id field with an automated integer as a primary key to see if I could track whether communication was really coming into the database from the form, and sure enough, each time I hit "submit" on the form, a new row is created in the database with the next chronological number - but all fields afterward are blank. Here is my code I am using: Form Code <form action="form-data.php" method=”post”> <p><label>Enter full name: </label><input type=”text” name=”fullname” size="30" /></p> <p><label>Sex: </label><input type=”text” name=”sex” size="10" /></p> <p><label>Choose an Option: </label><select name="dropdown"> <option selected="selected"></option> <option value="lame">Lame</option> <option value="garbage">Garbage</option> <option value="stupid">Stupid</option> </select></p> <input type="submit" value="Click!"> </form> And the php: <?php $con = mysql_connect("xxxx.db.xxxx.hostedresource.com","database_name","password"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("database_name", $con); $sql="INSERT INTO database_name.table1 (fullname, sex, dropdown) VALUES ('$_POST[fullname]','$_POST[sex]','$_POST[dropdown]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added"; mysql_close($con) ?> I am using MySQL version 5.0. I am accessing phpmyadmin through Godaddy (my host). I am receiving no errors. I created my database table using phpmyadmin, so I do not have a CREATE TABLE code for you, but here is a screencap of what my table looks like after I perform an SQL query: Notice that the "id" column is filling each time I submit, so it seems to be functioning (note - I manually deleted numbers 1-26). Again, I want my submitted information to show in the database. Other than the data not appearing, everything else seems to be working properly. I have tried different variations of code on the internet to try to solve this problem, but they all return errors. I am open to trying any suggestions that anyone here might have. I run a few different websites all on the same hosting plan and I have created databases on each to try to test this out, and in every instance information is submitted but not recorded to the database. What am I doing wrong? [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/210449-form-data-not-appearing-in-mysql-database/ Share on other sites More sharing options...
jonsjava Posted August 11, 2010 Share Posted August 11, 2010 try this: $sql="INSERT INTO database_name.table1 (fullname, sex, dropdown) VALUES ('{$_POST[fullname]}','{$_POST[sex]}','{$_POST[dropdown]}')"; I would recommend that you sanitize your inputs first, though: $fullname = mysql_real_escape_string($_POST['fullname']); $sex = mysql_real_escape_string($_POST['sex']); $dropdown = mysql_real_escape_string($_POST['dropdown']); $sql = "INSERT INTO `table1` VALUES (NULL,'$fullname','$sex','$dropdown');"; Quote Link to comment https://forums.phpfreaks.com/topic/210449-form-data-not-appearing-in-mysql-database/#findComment-1098132 Share on other sites More sharing options...
Skelly123 Posted August 11, 2010 Author Share Posted August 11, 2010 Hi jonsjava, Thanks so much for giving me the time of day! I did precisely as you instructed. Here is what my php looks like now: <?php $con = mysql_connect("xxxx.db.xxxx.hostedresource.com","database_name","password"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("database_name", $con); $fullname = mysql_real_escape_string($_POST['fullname']); $sex = mysql_real_escape_string($_POST['sex']); $dropdown = mysql_real_escape_string($_POST['dropdown']); $sql = "INSERT INTO `table1` VALUES (NULL,'$fullname','$sex','$dropdown');"; $sql="INSERT INTO database_name.table1 (fullname, sex, dropdown) VALUES ('{$_POST[fullname]}','{$_POST[sex]}','{$_POST[dropdown]}')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added"; mysql_close($con) ?> So, here is the effect it had. The echo "1 record added" still appeared after submission with no errors. My data still did not appear in the database, but what happened was that the new submission (#30) was placed at the beginning of the table (the top) as opposed to the end (the bottom). Here is a screen cap to illustrate: Previously the new data was being placed at the bottom of the table in chronological order. So unfortunately, this is still a mystery that remains unsolved. Quote Link to comment https://forums.phpfreaks.com/topic/210449-form-data-not-appearing-in-mysql-database/#findComment-1098199 Share on other sites More sharing options...
wildteen88 Posted August 11, 2010 Share Posted August 11, 2010 Post your table structure here. Your code is fine, however placing raw $_POST values into a query is not recommended for security reasons. You should be sanitizing any user input, as suggested by jonsjava earlier. Quote Link to comment https://forums.phpfreaks.com/topic/210449-form-data-not-appearing-in-mysql-database/#findComment-1098203 Share on other sites More sharing options...
Skelly123 Posted August 11, 2010 Author Share Posted August 11, 2010 Hi Wild Teen, Thank you for responding. Here is the result of "describer table1." I hope this is what you were asking for. By using the php code in my previous post, was I sanitizing properly? Or is there something else I need to do to the php code still? Now that I know sanitizing is simply good practice, I'll be sure to add in the relevant code in the future. Quote Link to comment https://forums.phpfreaks.com/topic/210449-form-data-not-appearing-in-mysql-database/#findComment-1098209 Share on other sites More sharing options...
wildteen88 Posted August 11, 2010 Share Posted August 11, 2010 Just noticed your form <form action="form-data.php" method=”post”> The ” is invalid HTML, it should be a ". Change the quotes and your code should run as expected. Quote Link to comment https://forums.phpfreaks.com/topic/210449-form-data-not-appearing-in-mysql-database/#findComment-1098213 Share on other sites More sharing options...
PFMaBiSmAd Posted August 11, 2010 Share Posted August 11, 2010 ^^^ There's at least 5 values in the form HTML that are surrounded by curly/smart quotes and would need to be corrected. Quote Link to comment https://forums.phpfreaks.com/topic/210449-form-data-not-appearing-in-mysql-database/#findComment-1098216 Share on other sites More sharing options...
Skelly123 Posted August 11, 2010 Author Share Posted August 11, 2010 Ok, I dumped the code into MS Word to be able to see the smart quotes better. I changed all the smart quotes in Notepad so they look right. FTP'd the file in. No change - info still not appearing in database. Here is the form code now. <form action="form-data.php" method="post"> <p><label>Enter full name: </label><input type="text" name="fullname" size="30" /></p> <p><label>Sex: </label><input type="text" name="sex" size="10" /></p> <p><label>Choose an Option: </label><select name="dropdown"> <option selected="selected"></option> <option value="lame">Lame</option> <option value="garbage">Garbage</option> <option value="stupid">Stupid</option> </select></p> <input type="submit" value="Click!"> </form> Boy, I was really hoping that would be it. Quote Link to comment https://forums.phpfreaks.com/topic/210449-form-data-not-appearing-in-mysql-database/#findComment-1098220 Share on other sites More sharing options...
PFMaBiSmAd Posted August 11, 2010 Share Posted August 11, 2010 The form you posted works as expected and submits the three $_POST['...'] variables - POST:Array ( [fullname] => pfmabismad [sex] => yes [dropdown] => lame ) If it didn't work for you, are you sure you updated the correct file and refreshed the form in your browser so that any changes made to the HTML source would take effect? Quote Link to comment https://forums.phpfreaks.com/topic/210449-form-data-not-appearing-in-mysql-database/#findComment-1098282 Share on other sites More sharing options...
Skelly123 Posted August 11, 2010 Author Share Posted August 11, 2010 Hi PFMaBiSmAd, Thanks for trying out the code independently. There must apparently be something going wrong with the MySQL I am using via Starfield Technologies (the company Godaddy uses for phpmyadmin). It's the only thing I can think of if the code works for you and not me. I did update the form. You can view it at the following URL. http://mtgcodex.com/testdatabase/form.htm As I said before, the database is registering the receipt of information, but then (as shown in my screen caps), no data appears other than the primary key. Any other ideas? Quote Link to comment https://forums.phpfreaks.com/topic/210449-form-data-not-appearing-in-mysql-database/#findComment-1098289 Share on other sites More sharing options...
PFMaBiSmAd Posted August 11, 2010 Share Posted August 11, 2010 Add the following debugging code to your form-data.php code to see if the $_POST data is being received - echo "<pre>"; echo "POST:"; print_r($_POST); echo "</pre>"; Quote Link to comment https://forums.phpfreaks.com/topic/210449-form-data-not-appearing-in-mysql-database/#findComment-1098291 Share on other sites More sharing options...
Skelly123 Posted August 11, 2010 Author Share Posted August 11, 2010 Woah! Ok, I entered in the debugging code into the php, filled out the form, pressed the submit button, and here is what appeared on the next screen: POST:Array ( [fullname] => Bitch [sex] => Male [dropdown] => stupid ) 1 record added Then I checked the daabase, and voila! It posted! Now I'm going to remove that debugging code from the php and try again. Give me a few minutes. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/210449-form-data-not-appearing-in-mysql-database/#findComment-1098300 Share on other sites More sharing options...
Skelly123 Posted August 11, 2010 Author Share Posted August 11, 2010 Alright, well, I removed the debugging code from the original php, and now the form is posting to the database terrifically without it. Case closed! Thank you, everyone, for helping me! Quote Link to comment https://forums.phpfreaks.com/topic/210449-form-data-not-appearing-in-mysql-database/#findComment-1098301 Share on other sites More sharing options...
PFMaBiSmAd Posted August 12, 2010 Share Posted August 12, 2010 I'm going to guess that because you are attempting to develop code on a live server, instead of a local development system, that all the disk/web server caching that the web host has setup to improve his server efficiency is causing your changing code to take a while to actually get used on the server. You should develop code locally and only put final finished code onto a live server. You will save a ton of your time. Quote Link to comment https://forums.phpfreaks.com/topic/210449-form-data-not-appearing-in-mysql-database/#findComment-1098488 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.