wabash Posted May 16, 2011 Share Posted May 16, 2011 Hi there, I'm inserting data into a mysql database. My submissions works fine, however when a part of the form is not filled out the php scripit crashes. How can I submit all data that is set and skip pieces that aren't filled out? For example I have a plant database which has an option to insert different types of common names for the plant. It's ok if they aren't all filled out. My code is as follows: <?php //insert general plant data into db $queryplant="INSERT INTO actualplanttable (id, scientific, common1, common2, common3, family, genus, description, planting, growing, care, duration, reference) VALUES ('NULL', '$_POST[scientific]', '$_POST[common1]', '$_POST[common2]', '$_POST[common3]', '$_POST[family]', '$_POST[genus]', '$_POST[description]', '$_POST[planting]', '$_POST[growing]', '$_POST[care]','$_POST[duration]','$_POST[reference]')"; mysql_query($queryplant) or die ('Error updating actualplanttable'); //insert maintenance data into db $querymaintenance="INSERT INTO plantmaintenancelinktable (plant_id, maintenance_id) VALUES ('$inserted_id', '$_POST[maintenance]')"; mysql_query($querymaintenance) or die ('Error updating plantdurationtable'); ?> Should I be trying to work an isset function into this code? Any help would be greatly appreciated. Thanks! Bill... Quote Link to comment https://forums.phpfreaks.com/topic/236557-isset-help/ Share on other sites More sharing options...
ohdang888 Posted May 16, 2011 Share Posted May 16, 2011 use: or die (mysql_error().' in updating actualplanttable'); Quote Link to comment https://forums.phpfreaks.com/topic/236557-isset-help/#findComment-1216073 Share on other sites More sharing options...
wabash Posted May 16, 2011 Author Share Posted May 16, 2011 thanks odang! That worked for the text box submissions, though when I get a bit further down in the code to the check box submission area of my code--which is using a foreach statement, I still get errors for not inserting data: I tried using the error code you provided: <?php //insert exposure requirements foreach ($_POST['exposure'] as $value) { $queryexposure="INSERT INTO plantexposurelinktable (plant_id, exposure_id) VALUES ('$inserted_id','$value')"; mysql_query($queryexposure) or die (mysql_error().' in updating plantexposurelinktable'); } ?> The error is....... Warning: Invalid argument supplied for foreach() in ..... on line 51. Any ideas about this? Continued thanks for your help. Bill... Quote Link to comment https://forums.phpfreaks.com/topic/236557-isset-help/#findComment-1216089 Share on other sites More sharing options...
KevinM1 Posted May 16, 2011 Share Posted May 16, 2011 It's telling you that $_POST['exposure'] is not an array, which means foreach() cannot work on it. You need to check if it's null. Quote Link to comment https://forums.phpfreaks.com/topic/236557-isset-help/#findComment-1216097 Share on other sites More sharing options...
wabash Posted May 16, 2011 Author Share Posted May 16, 2011 Thanks Nightslyr, Yes, I'm leaving this section blank and not getting the array. I'm hoping to leave sections blank but still be able to submit other sections of the form. Can you tell me how to check if it's null? Or point me in the right direction? I've never done that before. Continued thanks. Bill.... Quote Link to comment https://forums.phpfreaks.com/topic/236557-isset-help/#findComment-1216100 Share on other sites More sharing options...
KevinM1 Posted May 16, 2011 Share Posted May 16, 2011 Try something along the lines of: if (!empty($_POST['exposure']) && is_array($_POST['exposure'])) { // run foreach } else { // error } The empty part of the conditional checks to see if $_POST['exposure'] actually contains a value. The is_array portion checks to see if the value is an array you can loop over. Quote Link to comment https://forums.phpfreaks.com/topic/236557-isset-help/#findComment-1216102 Share on other sites More sharing options...
wabash Posted May 16, 2011 Author Share Posted May 16, 2011 Yes! Thanks so much for this Nightslyr. Much appreciated. Bill.. Quote Link to comment https://forums.phpfreaks.com/topic/236557-isset-help/#findComment-1216176 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.