Jump to content


Photo

Notice: undefined index.....


  • Please log in to reply
6 replies to this topic

#1 benjamin_boothe

benjamin_boothe
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 21 August 2006 - 12:09 PM

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 = "";
}
?>

#2 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 21 August 2006 - 12:16 PM

For a start you're trying to insert 17 values into 18 fields.

Rich
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#3 ScottRiley

ScottRiley
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 21 August 2006 - 12:19 PM

Yeah, you're missong a variable for 'title'

#4 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 21 August 2006 - 12:21 PM

you're missing a variable for 'title'


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
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#5 ScottRiley

ScottRiley
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 21 August 2006 - 12:25 PM

If your ID is set to auto_increment, use the value 0 as opposed to null.

#6 benjamin_boothe

benjamin_boothe
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 21 August 2006 - 01:59 PM

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?



#7 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 21 August 2006 - 02:20 PM

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
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users