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
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
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
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
Share on other sites

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.