kirkenall Posted January 11, 2010 Share Posted January 11, 2010 Hi good people. I am trying to put together a simple application where I take info from an html form and insert it into a mySQL db. everything appears to work proper but a new record is never actually created in the db. Below is the PHP code. Thanks in advance for any assistance. <?php $con = mysql_connect ("localhost", "root", "xxxx"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db ("registrants")or die(mysql_error()); $ebits = ini_get('error_reporting'); error_reporting($ebits ^ E_NOTICE); $firstname =$_POST["firstname"]; $lastname = $_POST["lastname"]; $dateofbirth =$_POST["dateofbirth"]; $ss = $_POST["ss"]; $address = $_POST["address"]; $apt = $_POST["apt"]; $city = $_POST["city"]; $state =$_POST["state"]; $zip = $_POST["zip"]; $email = $_POST["email"]; $homephone = $_POST["homephone"]; $cell = $_POST["cell"]; $Sex = $_POST["Sex"]; $maritalstat = $_POST["maritalstat"]; $race = $_POST["race"]; $citizen = $_POST["citizen"]; mysql_query ("INSERT INTO registrants (first name, last name, date of birth, ss#, address, city, state, zipcode, email, homephone, cellphone, sex, marriage, ethnicity, citizen, apt#, id) Values ('$firstname', '$lastname', '$dateofbirth', '$ss', '$address', '$city', '$state', '$zip', '$email', '$homephone', '$cell', '$Sex', '$maritalstat', '$race', '$citizen', '$apt', 'NULL')"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/188082-insert-help-for-newbie/ Share on other sites More sharing options...
kirkenall Posted January 11, 2010 Author Share Posted January 11, 2010 Here is the form info: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Registration</title> <style type="text/css"> <!-- body { background-color: #F90; font-size: medium; } #form1 table tr td div label { color: #FFF; } #form1 table { color: #FFF; font-size: 24px; } --> </style></head> <body> <form id="form1" name="form1" method="post" action="index.php"> <label><br /> </label> <table width="100%" border="0" cellspacing="2" cellpadding="2"> <tr> <td><label for="firstname"> <div align="right">First Name</div> </label> </td> <td><div align="left"> <input name="firstname" type="text" id="firstname" size="25" maxlength="40" /> </div></td> </tr> <tr> <td><label for="lastname"> <div align="right">Last Name</div> </label> </td> <td><div align="left"> <input name="lastname" type="text" id="lastname" size="25" /> </div></td> </tr> <tr> <td><label for="dateofbirth"> <div align="right">Date of Birth</div> </label> </td> <td><div align="left"> <input name="dateofbirth" type="text" id="dateofbirth" size="25" /> </div></td> </tr> <tr> <td><label for="ss"> <div align="right">Social Security #</div> </label> </td> <td><div align="left"> <input name="ss" type="text" id="ss" size="25" maxlength="11" /> </div></td> </tr> <tr> <td><label for="address"> <div align="right">Address</div> </label> </td> <td><div align="left"> <input name="address" type="text" id="address" size="50" /> </div></td> </tr> <tr> <td><label for="apt"> <div align="right">Apt#</div> </label> </td> <td><div align="left"> <input name="apt" type="text" id="apt" size="8" /> </div></td> </tr> <tr> <td><label for="city"> <div align="right">City</div> </label> </td> <td><div align="left"> <input name="city" type="text" id="city" value="Brooklyn" size="25" /> </div></td> </tr> <tr> <td><label for="state"> <div align="right">State</div> </label> </td> <td><div align="left"> <input name="state" type="text" id="state" value="New York" size="25" /> </div></td> </tr> <tr> <td><label for="zip"> <div align="right">Zipcode</div> </label> </td> <td><div align="left"> <input name="zip" type="text" id="zip" size="25" /> </div></td> </tr> <tr> <td><label for="email"> <div align="right">email Address</div> </label> </td> <td><div align="left"> <input name="email" type="text" id="email" size="50" /> </div></td> </tr> <tr> <td><label for="homephone"> <div align="right">Home Phone</div> </label> </td> <td><div align="left"> <input name="homephone" type="text" id="homephone" size="25" /> </div></td> </tr> <tr> <td><label for="cell"> <div align="right">Cell Phone</div> </label> </td> <td><div align="left"> <input name="cell" type="text" id="cell" size="25" /> </div></td> </tr> <tr> <td height="28"><label for="sex"> <div align="right">Sex</div> </label> </td> <td><div align="left"> <p> <label> <input type="radio" name="Sex" id="Sex_0" value="0" checked="checked" /> Male</label> <label> <input type="radio" name="Sex" id="Sex_1" value="1" /> Female</label> <br /> </p> </div></td> </tr> <tr> <td><label for="maritalstat"> <div align="right">Marital Status</div> </label> </td> <td><select name="maritalstat" size="1" id="maritalstat"> <option value="1">Never Married</option> <option value="2">Divorced</option> <option value="3">Widowed</option> <option value="4">Separated</option> <option value="5">Married</option> </select></td> </tr> <tr> <td><div align="right"> <label for="race">Race/Ethnicity</label> </div></td> <td><select name="race" size="1" id="race"> <option value="1">African American</option> <option value="2">Hispanic or Latino</option> <option value="3">American Indian or Native Alaskan</option> <option value="4">White or Caucasian</option> <option value="5">Multi-racial</option> <option value="6">Asian Pacific Islander</option> <option value="7">Other</option> </select></td> </tr> <tr> <td height="28"><div align="right"> <p><label>Are you a US Citizen?</label><br /> </p> </div></td> <td><input type="radio" name="citizen" id="citizen_0" value="0" checked="checked" /> <label>Yes <input type="radio" name="citizen" id="citizen_1" value="1" /> No </label></td> </tr> <tr> <td> </td> <td> </td> </tr> <tr> <td><div align="right"> <input type="reset" name="clear" id="clear" value="Reset form" /> </div></td> <td><label for="Submit"></label> <input type="submit" name="Submit" id="Submit" value="Submit" /></td> </tr> </table> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/188082-insert-help-for-newbie/#findComment-992946 Share on other sites More sharing options...
Catfish Posted January 11, 2010 Share Posted January 11, 2010 check your mysql query works with an if() conditional and output the error on failure: if (mysql_query ("INSERT INTO registrants (first name, last name, date of birth, ss#, address, city, state, zipcode, email, homephone, cellphone, sex, marriage, ethnicity, citizen, apt#, id) Values ('$firstname', '$lastname', '$dateofbirth', '$ss', '$address', '$city', '$state', '$zip', '$email', '$homephone', '$cell', '$Sex', '$maritalstat', '$race', '$citizen', '$apt', 'NULL')")) { // succeeded. continue with code } else print("Could not INSERT data to database. Error given: ".mysql_error()."<br/>\n"); the link in my signature has a short topic on flow control which shows you this method to validate all your script's actions as you go. it's a handy way to code. Quote Link to comment https://forums.phpfreaks.com/topic/188082-insert-help-for-newbie/#findComment-992947 Share on other sites More sharing options...
Adam Posted January 11, 2010 Share Posted January 11, 2010 Here's your problem: mysql_query ("INSERT INTO registrants (first name, last name, date of birth, ss#, address, city, state, zipcode, email, homephone, cellphone, sex, marriage, ethnicity, citizen, apt#, id) Values ('$firstname', '$lastname', '$dateofbirth', '$ss', '$address', '$city', '$state', '$zip', '$email', '$homephone', '$cell', '$Sex', '$maritalstat', '$race', '$citizen', '$apt', 'NULL')"); Quote Link to comment https://forums.phpfreaks.com/topic/188082-insert-help-for-newbie/#findComment-992949 Share on other sites More sharing options...
kirkenall Posted January 11, 2010 Author Share Posted January 11, 2010 Catfish---thanks for the reply, I tried your code and received the following error: Could not INSERT data to database. Error given: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'name, last name, date of birth, ss#, address, city, state, zipcode, email, homep' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/188082-insert-help-for-newbie/#findComment-992961 Share on other sites More sharing options...
kirkenall Posted January 11, 2010 Author Share Posted January 11, 2010 MrAdam---thank you too for the reply....I tried your code too and still the same results .....no record is created in the db. Quote Link to comment https://forums.phpfreaks.com/topic/188082-insert-help-for-newbie/#findComment-992962 Share on other sites More sharing options...
MatthewJ Posted January 11, 2010 Share Posted January 11, 2010 field names with spaces must be enclosed with backticks (`) Quote Link to comment https://forums.phpfreaks.com/topic/188082-insert-help-for-newbie/#findComment-992965 Share on other sites More sharing options...
kirkenall Posted January 11, 2010 Author Share Posted January 11, 2010 MattewJ....I am not seeing the field names with spaces......I do not want spaces Quote Link to comment https://forums.phpfreaks.com/topic/188082-insert-help-for-newbie/#findComment-992968 Share on other sites More sharing options...
MatthewJ Posted January 11, 2010 Share Posted January 11, 2010 "INSERT INTO registrants (first name, last name, date of birth, ss#, address, city, state, zipcode, email, homephone, cellphone, sex, marriage, ethnicity, citizen, apt#, id) Values ('$firstname', '$lastname', '$dateofbirth', '$ss', '$address', '$city', '$state', '$zip', '$email', '$homephone', '$cell', '$Sex', '$maritalstat', '$race', '$citizen', '$apt', 'NULL')" Quote Link to comment https://forums.phpfreaks.com/topic/188082-insert-help-for-newbie/#findComment-992970 Share on other sites More sharing options...
kirkenall Posted January 11, 2010 Author Share Posted January 11, 2010 MatthewJ....Same thing. After I changed the code to your rec. I still receive the following after running the code and then checking for a new record in PHPMyAdmin...............................MySQL returned an empty result set (i.e. zero rows). Quote Link to comment https://forums.phpfreaks.com/topic/188082-insert-help-for-newbie/#findComment-992973 Share on other sites More sharing options...
Lamez Posted January 11, 2010 Share Posted January 11, 2010 in your database, edit the column with the # sign, remove it, then edit your query. Quote Link to comment https://forums.phpfreaks.com/topic/188082-insert-help-for-newbie/#findComment-992977 Share on other sites More sharing options...
kirkenall Posted January 11, 2010 Author Share Posted January 11, 2010 I edited out the # sign and am still getting the same result. No record is added to the db when the script is ran. Quote Link to comment https://forums.phpfreaks.com/topic/188082-insert-help-for-newbie/#findComment-992982 Share on other sites More sharing options...
kirkenall Posted January 11, 2010 Author Share Posted January 11, 2010 Thanks for all of the help.....you people are great. The problem had to do with having spaces in the column names. Quote Link to comment https://forums.phpfreaks.com/topic/188082-insert-help-for-newbie/#findComment-993009 Share on other sites More sharing options...
Catfish Posted January 12, 2010 Share Posted January 12, 2010 rofl. that's what he said 6 posts back Quote Link to comment https://forums.phpfreaks.com/topic/188082-insert-help-for-newbie/#findComment-993330 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.