Eiolon Posted July 4, 2011 Share Posted July 4, 2011 I have a two part form, because I have to insert into two different tables. First table is event information. Second table is the intended audience. If first part of the form inserts successfully into events table, it moves on to process the audience table. If no selections are made for the audience, it errors out as "Warning: Invalid argument supplied for foreach()" If a selection is made, it works fine. I don't want to make selecting a target audience a requirement so I want it to forward to the confirmation page as if I did make a selection. if (empty($errors)) { $insert_program = "INSERT INTO programs (name, coordinator, description, instructions, online_option, session_limit) VALUES ('$n','$c','$d','$i','$oo','$sl')"; $result_program = mysql_query($insert_program) OR die ('Could not create the new program.'); $id = mysql_insert_id(); if ($insert_program) { foreach($_POST['audience'] as $aid) { if ($aid > 0) { $insert_audience = "INSERT INTO programs_audiences (program_id, audience_id) VALUES ('$id','$aid')"; $result_audience = mysql_query($insert_audience) OR die ('Could not insert the intended audiences.'); if ($insert_audience) { header("Location: program_created.php?id=$id"); exit; } } else if ($aid < 1) { header("Location: program_created.php?id=$id"); exit; } } } } Quote Link to comment https://forums.phpfreaks.com/topic/241093-telling-checkbox-array-to-not-insert-if-no-selections-made/ Share on other sites More sharing options...
EdwinPaul Posted July 4, 2011 Share Posted July 4, 2011 if ($insert_program) { if ($insert_audience) { are no booleans. If $_POST['audience'] is your checkbox, I think it would be wise to check it with isset($_POST['audience']) and with is_array($_POST['audience']) Quote Link to comment https://forums.phpfreaks.com/topic/241093-telling-checkbox-array-to-not-insert-if-no-selections-made/#findComment-1238341 Share on other sites More sharing options...
Eiolon Posted July 5, 2011 Author Share Posted July 5, 2011 Thanks. I understand now. It's working. Quote Link to comment https://forums.phpfreaks.com/topic/241093-telling-checkbox-array-to-not-insert-if-no-selections-made/#findComment-1238394 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.