Jump to content

Archived

This topic is now archived and is closed to further replies.

benjamin_boothe

Soooo Fustrating!!!!!!!!!!!! - Error Inserting Data to db via Form

Recommended Posts

I need help!!

My form is not submitting the data to db. I just keep getting msg 'Error inserting data' which was a comment in my if statment.  So it is working in that respect.  But I want to actually carry out the creating the record successfully.

I can add a record manually but that's no good to me.  So there's got  be a problem with my form but I cannot see what!!

Here is the code:

<META content="MSHTML 6.00.2800.1458" name=GENERATOR></HEAD>
<BODY bgColor=#99ccff>
<DIV><B>GSI Insurance LTD</B> (Customer Contact Page)</DIV>
<HR>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">

<?

$t = isSet($_POST['t']) ? $_POST['t'] : '';
$fn = isSet($_POST['fn']) ? $_POST['fn'] : '';
$sn = isSet($_POST['sn']) ? $_POST['sn'] : '';
$dob = isSet($_POST['dob']) ? $_POST['dob'] : '';
$uk = isSet($_POST['uk']) ? $_POST['uk'] : '';
$ms = isSet($_POST['ms']) ? $_POST['ms'] : '';
$d = isSet($_POST['d']) ? $_POST['d'] : '';
$es = isSet($_POST['es']) ? $_POST['es'] : '';
$ad1 = isSet($_POST['ad1']) ? $_POST['ad1'] : '';
$a = isSet($_POST['a']) ? $_POST['a'] : '';
$tn = isSet($_POST['tn']) ? $_POST['tn'] : '';
$c = isSet($_POST['c']) ? $_POST['c'] : '';
$pc = isSet($_POST['pc']) ? $_POST['pc'] : '';
$tw = isSet($_POST['tw']) ? $_POST['tw'] : '';
$th = isSet($_POST['th']) ? $_POST['th'] : '';
$fax = isSet($_POST['fax']) ? $_POST['fax'] : '';
$em = isSet($_POST['em']) ? $_POST['em'] : '';

// has form been submitted?
if ($_POST) {
  // create empty error variable
  $msg = "";
 
  // put form data into variable variables
  foreach($_POST as $k => $v) {
    $v = trim($v) ;
    $$k = $v ;
     
    // check for data in both fields
    if ($v=="") {
      $msg = "Please fill in all fields";
    }
  }

  // if all data is there, build query
  if ($msg=="") {
    $insert = "INSERT INTO policy_holder
    (id, title, first_name, surname, date_of_birth, uk_residency, marital_status,
    disability, employment_status, address_line_1, area, town, county, post_code,
    telephone_work, telephone_home, fax_number, email)
    VALUES ( NULL, '$t', '$fn', '$sn', '$dob', '$uk', '$ms', '$d', '$es', '$ad1', '$a',
    '$tn', '$c', '$pc', '$tw', '$th', '$fax', '$em')";

    // open db connection
    include 'includes/db_conn.txt';

    // execute query and check for success
    if (!mysqli_query($link, $insert)) {
      $msg = "Error inserting data";
    } else {
      $msg = "Record succesfully added";
      // set vars to "" for next form input
      $t = $fn = $sn = $dob = $uk = $ms = $d = $es = $ad1 = $a = $tn = $c = $pc = $tw = $th =
      $fax = $em = "";
    }
    mysqli_close($link);
  }

  // print error or success messages
  echo "<div class=\"error\">$msg</div>";
 
// if not submitted, create blank vars for form inputs
} else {
  $t = $fn = $sn = $dob = $uk = $ms = $d = $es = $ad1 = $a = $tn = $c = $pc = $tw = $th =
  $fax = $em = "";
}
?>

<FORM METHOD="post" ACTION="<? echo $_SERVER['PHP_SELF'] ?>">
<TABLE cellSpacing=0 cellPadding=5 border=0>

<TR>
    <TD vAlign=top align=left>Title </TD>
    <TD vAlign=top align=left><SELECT size=1 name="title" VALUE="<? echo $t ?>"/> <OPTION
        value="" selected>-- Please select --</OPTION> <OPTION
        value=Mr>Mr</OPTION> <OPTION value=Mrs>Mrs</OPTION> <OPTION
        value=Miss>Miss</OPTION> <OPTION value=MS>MS</OPTION></SELECT> <BR></TD></TR>
  <TR>
    <TD vAlign=top align=left>First Name </TD>
    <TD vAlign=top align=left><INPUT TYPE="text" name="first_name" VALUE="<? echo $fn ?>"/>
    <BR></TD></TR>
  <TR>
    <TD vAlign=top align=left>Last Name </TD>
    <TD vAlign=top align=left><INPUT TYPE="text" name="surname" VALUE="<? echo $sn ?>"/>
    <BR></TD></TR>
  <TR>
    <TD vAlign=top align=left>Date Of Birth </TD>
    <TD vAlign=top align=left><INPUT TYPE="text" name="date_of_birth" VALUE="<? echo $dob ?>"/>
    <BR></TD></TR>
  <TR>
    <TD vAlign=top align=left>UK Residency </TD>
    <TD vAlign=top align=left><SELECT size=1 name="uk_residency" VALUE="<? echo $uk ?>"/> <OPTION
        value="" selected>-- Please select --</OPTION> <OPTION
        value="From Birth">From Birth</OPTION> <OPTION value="1 to 5 Years">1 to
        5 Years</OPTION> <OPTION value="5 to 10 Years">5 to 10 Years</OPTION>
        <OPTION value="10 or more Years">10 or more Years</OPTION></SELECT>
  <BR></TD></TR>
  <TR>
    <TD vAlign=top align=left>Marital Status </TD>
    <TD vAlign=top align=left><SELECT size=1 name="marital_status" VALUE="<? echo $ms ?>"/>
    <OPTION value="" selected>-- Please select --</OPTION>
    <OPTION value=Married>Married</OPTION>
    <OPTION value=Single>Single</OPTION></SELECT> <BR></TD></TR>
     

  <TR>
    <TD vAlign=top align=left>Disability (or None) </TD>
    <TD vAlign=top align=left><INPUT TYPE="text" name="disability" VALUE="<? echo $d ?>"/>
    <BR></TD></TR>
  <TR>
    <TD vAlign=top align=left>Employment Status</TD>
    <TD vAlign=top align=left><SELECT size=1 name="employment_status" VALUE="<? echo $es ?>"/>
    <OPTION value="" selected>-- Please select --</OPTION>
    <OPTION value=Self Employed>Self Employed</OPTION>
    <OPTION value=Government>Government</OPTION>
    <OPTION value=Other>Other</OPTION></SELECT> <BR></TD></TR>
     
  <TR>
    <TD vAlign=top align=left>Address Line 1 </TD>
    <TD vAlign=top align=left><INPUT TYPE="text" name="address_line_1" VALUE="<? echo $ad1 ?>"/>
    <BR></TD></TR>
  <TR>
    <TD vAlign=top align=left>Area </TD>
    <TD vAlign=top align=left><INPUT TYPE="text" name="area" VALUE="<? echo $a ?>"/>
    <BR></TD></TR>
  <TR>
    <TD vAlign=top align=left>Town </TD>
    <TD vAlign=top align=left><INPUT TYPE="text" name="town" VALUE="<? echo $tn ?>"/>
    <BR></TD></TR>
  <TR>
    <TD vAlign=top align=left>County </TD>
    <TD vAlign=top align=left><INPUT TYPE="text" name="county" VALUE="<? echo $c ?>"/>
    <BR></TD></TR>
  <TR>
    <TD vAlign=top align=left>Post Code </TD>
    <TD vAlign=top align=left><INPUT TYPE="text" name="post_code" VALUE="<? echo $pc ?>"/>
    <BR></TD></TR>
  <TR>
    <TD vAlign=top align=left>Telephone Work </TD>
    <TD vAlign=top align=left><INPUT TYPE="text" name="telephone_work" VALUE="<? echo $tw ?>"/>
    <BR></TD></TR>
  <TR>
    <TD vAlign=top align=left>Telephone Home </TD>
    <TD vAlign=top align=left><INPUT TYPE="text" name="telephone_home" VALUE="<? echo $th ?>"/>
    <BR></TD></TR>
  <TR>
    <TD vAlign=top align=left>Fax Number </TD>
    <TD vAlign=top align=left><INPUT TYPE="text" name="fax_number" VALUE="<? echo $fax ?>"/>
    <BR></TD></TR>
  <TR>
    <TD vAlign=top align=left>E-Mail </TD>
    <TD vAlign=top align=left><INPUT TYPE="text" name="email" VALUE="<? echo $em ?>"/>
<BR></TD></TR></TBODY></TABLE>

<P><INPUT type=submit value="Add To Database" name=""> <INPUT type=reset value="Cancel"> </P></FORM>
<HR>

Share this post


Link to post
Share on other sites
Chnage this:
[code]if (!mysqli_query($link, $insert)) {
      $msg = "Error inserting data";
    } else {[/code]
to:
[code]if (!mysqli_query($link, $insert)) {
      $msg = "Error inserting data - " . mysqli_error($link);
    } else {[/code]
It should now show an error messge from MySQL with info on why it is failing. Post the error here if you dont know what it means.

Share this post


Link to post
Share on other sites
Thanks Wildteen88

Incorrect date value: '' for column 'date_of_birth' at row 1

That is the error message I am getting now.  I have took this on board and typed it in the form in this format: YYYY-MM-DD.  That is the DATE format, which is the character type for this field.

Because I have it as "text" as an input type on my form, does this make a difference?

Share this post


Link to post
Share on other sites

×

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.