Jump to content

Archived

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

benjamin_boothe

Notice: undefined index.....

Recommended Posts

I keep getting those annoying mesaage.

I am using the $_POST variable for my defining variables.

This is the php code I am using:

<?
// 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, '$fn', '$sn', '$dob', '$uk', '$ms', '$d', '$es', '$ad1', '$a',
    '$t', '$c', '$pc', '$tw', '$th', '$fax', '$em')";
   
    // open db connection
    include 'includes/db_conn.php';
   
    // 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
      $fn = $sn = $dob = $uk = $ms = $d = $es = $ad1 = $a = $t = $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 {
  $fn = $sn = $dob = $uk = $ms = $d = $es = $ad1 = $a = $t = $c = $pc = $tw = $th =
      $fax = $em = "";
}
?>

Share this post


Link to post
Share on other sites
[quote]
you're missing a variable for 'title'
[/quote]

And depending on how your database is setup, you probably aren't allowed to insert 'null' into the ID field.

This looks as though it's a sql/database error as opposed to PHP.  It's best to try the query in your database first, make sure it works, then code the PHP around it.

Rich

Share this post


Link to post
Share on other sites
Thanks guys,

But I have still have a problem whereby I cannot enter data into the db via my web form - It keeps stating that 'Error inserting data'.  I dont why this is.  I even was able to enter a record via command line SQL client.

I have option boxes on my form, could this be why I keep getting the errors?

Share this post


Link to post
Share on other sites
It's possible.

Echo your SQL statement to the screen before trying to insert it and see how it looks.  Then copy and paste that statement to insert into your DB.  If that doesn't work you know it's as a result of the form.  Possibly some invalid characters in one of the fields.

Regards
Rich

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.