Jump to content

Notice: undefined index.....


benjamin_boothe

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 = "";
}
?>
Link to comment
https://forums.phpfreaks.com/topic/18187-notice-undefined-index/
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
Link to comment
https://forums.phpfreaks.com/topic/18187-notice-undefined-index/#findComment-78015
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?

Link to comment
https://forums.phpfreaks.com/topic/18187-notice-undefined-index/#findComment-78073
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
Link to comment
https://forums.phpfreaks.com/topic/18187-notice-undefined-index/#findComment-78084
Share on other sites

Archived

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

×
×
  • 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.