benjamin_boothe Posted August 21, 2006 Share Posted August 21, 2006 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 = "";}?> Quote Link to comment https://forums.phpfreaks.com/topic/18187-notice-undefined-index/ Share on other sites More sharing options...
HuggieBear Posted August 21, 2006 Share Posted August 21, 2006 For a start you're trying to insert 17 values into 18 fields.Rich Quote Link to comment https://forums.phpfreaks.com/topic/18187-notice-undefined-index/#findComment-78013 Share on other sites More sharing options...
ScottRiley Posted August 21, 2006 Share Posted August 21, 2006 Yeah, you're missong a variable for 'title' Quote Link to comment https://forums.phpfreaks.com/topic/18187-notice-undefined-index/#findComment-78014 Share on other sites More sharing options...
HuggieBear Posted August 21, 2006 Share Posted August 21, 2006 [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 Quote Link to comment https://forums.phpfreaks.com/topic/18187-notice-undefined-index/#findComment-78015 Share on other sites More sharing options...
ScottRiley Posted August 21, 2006 Share Posted August 21, 2006 If your ID is set to auto_increment, use the value 0 as opposed to null. Quote Link to comment https://forums.phpfreaks.com/topic/18187-notice-undefined-index/#findComment-78017 Share on other sites More sharing options...
benjamin_boothe Posted August 21, 2006 Author Share Posted August 21, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/18187-notice-undefined-index/#findComment-78073 Share on other sites More sharing options...
HuggieBear Posted August 21, 2006 Share Posted August 21, 2006 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.RegardsRich Quote Link to comment https://forums.phpfreaks.com/topic/18187-notice-undefined-index/#findComment-78084 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.