Jump to content

Telling checkbox array to not insert if no selections made


Eiolon

Recommended Posts

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;
			}

		}

	}

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.