Jump to content

Insert Data into database from HTML form Failure


fueston

Recommended Posts

I have made a simple website for our local humane society and wish to include an html form which will feed the information into our mysql database.I am sure I had this working at one time, but in changing the html of the page somehow I have broken it and for the life of me I cannot seem to fix it. It will not put the data into the database.Here is the code from the html page (www.siskiyouhumane.org/webmemberform5.php)<form><form action="insert.php" method="post">Today's date (YYYY-MM-DD):<INPUT TYPE="text" NAME="date_entered" /><br />Last Name:<INPUT TYPE="text" NAME="last_name" /><br />First Name:<INPUT TYPE="text" NAME="first_name" /> <br /> Address:<INPUT TYPE="text" NAME="address" /> <br /> Town:<INPUT TYPE="text" NAME="town" /> <br />State (XX):<INPUT TYPE="text" NAME="state" /> <br /> Zip:<INPUT TYPE="text" NAME="zip" /> <br /> Phone (xxx xxx-xxxx) :<INPUT TYPE="text" NAME="phone" /><br />Email:<INPUT TYPE="text" NAME="email" /><br /> Do you have a special talent?:<INPUT TYPE="text" NAME="skills" /> <br /><INPUT TYPE="submit" value="Join now!" /></FORM></body></html>Here is the code from the insert file - the account name and database name are the same. The data table is called membership.<?php$con = mysql_connect("localhost","humane","password");if (!$con)mysql_select_db("humane", $con);$sql="INSERT INTO membership (date_entered, last_name, first_name, address, town, state, zip, phone, email, skills)VALUES('$_Post[date_entered]','$_POST[last_name]','$_POST[first_name]','$_Post[address]','$_Post[town]','$_Post[state]','$_Post[zip]','$_Post[phone]','$_Post','$_Post[skills]')";print "<p>Thank you for joining the Siskiyou Humane Society<p>";mysql_close($con);?> I hate to bother you very busy people, but I would greatly appreciate if anyone could see my mistake. I have spent hours trying to get this to work, looking at various sites and scripts. no luck.Thanks so much.Kathy Fueston

Link to comment
Share on other sites

I don't suppose you have the original version of the file, before you changed it? Can you remember what it was that you changed? Was in the php, or just the html?In any case, change this line...$con = mysql_connect("localhost","humane","password");to..$con = mysql_connect("localhost","humane","password") or die ('I cannot connect to the database because: ' . mysql_error());At the very least that should tell you why its not working.

Link to comment
Share on other sites

another debug tool is to perform a print_r() or a var_dump() of the $_POST array just before the INSERT command to see what is being submitted from the Form.*wait*The db is being connected to, the db is being selected, the SQL code is there, but the Query is never made.

 // Execute the query:$r = mysql_query ($con, $sql);// Print a message indicating success or not:if (mysql_affected_rows($con) ==1) {echo '<b><font color="green">The form information has been added!</font></b>';} else {echo '<b><font color="red">The form information could not be added!</font></b>';}

Add that code snippet just before the print() you have.(I think)

Link to comment
Share on other sites

Thank you for the suggestion.Tried putting this into my insert.php file and the webpage just flashes at me so I tried putting this line in a file named connecttest.php. When I run the file I get a blank page as a response. Should I add anything to the test file to make it respond with the error message?This is what my test page looks like:<?php$con = mysql_connect("localhost","humane","password") or die ('I cannot connect to the database because: ' . mysql_error()); mysql_close($con);?> Unfortunately the page the script was on has been changed a couple of times and I am not sure when I broke it. Stupidly, I did not test it as thoroughly as I should have along the way. Tried to put it back as close as I could back in June with the saved files I kept but no luck.

I don't suppose you have the original version of the file, before you changed it? Can you remember what it was that you changed? Was in the php, or just the html?In any case, change this line...$con = mysql_connect("localhost","humane","password");to..$con = mysql_connect("localhost","humane","password") or die ('I cannot connect to the database because: ' . mysql_error());At the very least that should tell you why its not working.
Link to comment
Share on other sites

if it's blank it's connecting... try this as your test file:

<?php$con = mysql_connect("localhost","humane","password");if ($con) mysql_select_db("humane", $con);$query= mysql_query("SELECT * FROM membership") or die("Could not complete query because " .mysql_error()); while($result=mysql_fetch_assoc($query)) {   print_r($result);   print '<br /><br />';} mysql_close($con);?>

Link to comment
Share on other sites

you have:
if (!$con)mysql_select_db("humane", $con);

which means if it's not connected > select database. so if it is connecting, it's not selecting the database...remove the '!' in the if.

Took your suggestion but no luck unfortunately. I dont think it is connecting so I am trying to come up with a test connection file. I tried this:
<?php$con = mysql_connect("localhost","humane","password") or die ('I cannot connect to the database because: ' . mysql_error()); mysql_close($con);?>

When I run this little test file, I get a blank page in response.kf

Link to comment
Share on other sites

if it's blank it's connecting... try this as your test file:
<?php$con = mysql_connect("localhost","humane","password");if ($con) mysql_select_db("humane", $con);$query= mysql_query("SELECT * FROM membership") or die("Could not complete query because " .mysql_error()); while($result=mysql_fetch_assoc($query)) {   print_r($result);   print '<br /><br />';} mysql_close($con);?>

This definately works. when I run this I get the whole database listed out. I am wondering if my problem has to do with the fields I am inserting. I have the list of fields in order with their values , but some of the fields in the database are not on the form and so are not listed in the insert statement. As long as I list the column list in correct order and with the correct name and corresponding values, do I need to list the fields not showing on the form in their appropriate order with values 'null' in the insert statement? In other words...form shows:nameaddresszipsame fields in our database are:member id (auto increment)name addresszipdonationwould the insert statement read:
insert into membership (null, name, address, zip, null)values   ('null ','$_POST[name]','$_Post[address]','$_Post[zip]', 'null')";

The auto increment field obviously does not show up on the form, nor does the donation field. Would they have to show up as 'null' on the insert statement?Thanks again for all your help.kf

Link to comment
Share on other sites

If this is what you actually have on the page:

<form><form action="insert.php" method="post">
Remove the first <form> tag. You can't have one form inside of another. And, by default, forms submit as get instead of post, so if the outer form is being submitted then your data will not go through post and you won't find it if you are looking there.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...