Jump to content

Form Data Not Appearing in MySQL Database


Skelly123

Recommended Posts

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: 

 

Untitled.jpg

 

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]

Link to comment
Share on other sites

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');";

Link to comment
Share on other sites

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:

 

Untitled1.jpg

 

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. :(

Link to comment
Share on other sites

Hi Wild Teen,

 

Thank you for responding.  Here is the result of "describer table1."  I hope this is what you were asking for.

 

Untitled2.jpg

 

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.

Link to comment
Share on other sites

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.  :P

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.