Jump to content

Recommended Posts

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

 

 

 

?>

Link to comment
https://forums.phpfreaks.com/topic/188082-insert-help-for-newbie/
Share on other sites

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>

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.

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

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

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

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.