Jump to content

[SOLVED] Problem with PHP & Multiple Select


mickinell

Recommended Posts

I apologize for my ignorance here--I'm teaching myself PHP and at times it's very frustrating.

 

I am trying to use multiple selects to pass certain information to a database.  On my "Add Lesson Plan" page, I have several multiple selects--one each for my classes, strategies, assessment, and associated unit plan.

 

For some reason, I have been able to correctly send the array to the database for two of those, but the classes and unit are saving as one value, not the set of values.

 

I've cut & pasted the relevant code from the php:

	$sql1 = "SELECT * FROM class ORDER BY class";
$result1 = @mysql_query($sql1,$connection) or die(mysql_error());
while ($row = mysql_fetch_array($result1)) {
 	$class = $row['class'];
	$name = $row['name'];
	$option_block1 .= "<option value=\"$class\">$name</option>";
	}
	$display_block1 .= "<select name=\"class[]\" id=\"class\" size=\"5\" style=\"width: 90%;\" multiple>$option_block1</select>";

$output .= $display_block1;
$output .= "<p><b>Instructional Activities/Assessment</b>";
	$sql3 = "SELECT * FROM strategy ORDER BY descript";
$result3 = @mysql_query($sql3,$connection) or die(mysql_error());
while ($row = mysql_fetch_array($result3)) {
 	$strat = $row['strat'];
	$descript = $row['descript'];
	$option_block3 .= "<option value=\"$strat\">$descript</option>";
	}
	$display_block3 .= "<select name=\"strat[]\" id=\"strat\" size=\"5\" style=\"width: 90%;\" multiple>$option_block3</select>";

$output .= $display_block3;

cols=75></textarea></p><p><b>Evaluation/Assessment:</b><br>";
$sql4 = "SELECT * FROM eval ORDER BY descript";
$result4 = @mysql_query($sql4,$connection) or die(mysql_error());
while ($row = mysql_fetch_array($result4)) {
 	$eval_id = $row['eval_id'];
	$descript = $row['descript'];
	$option_block4 .= "<option value=\"$eval_id\">$descript</option>";
	}
	$display_block4 .= "<select name=\"eval[]\" id=\"eval\" size=\"5\" style=\"width: 90%;\" multiple>$option_block4</select>";

$output .= $display_block4;

$sql5 = "SELECT * FROM unit ORDER BY unit";
$result5 = @mysql_query($sql5,$connection) or die(mysql_error());
while ($row = mysql_fetch_array($result5)) {
 	$unit = $row['unit'];
	$topic = $row['topic'];
	$class = $row['class'];
	$option_block5 .= "<option value=\"$unit\">$topic ($class)</option>";
	}
	$display_block5 .= "<select name=\"unit[]\" id=\"unit\" size=\"5\" style=\"width: 90%;\" multiple>$option_block5</select>";

$output .= $display_block5;

 

Okay.  Here's what I'm using to pass this all to the database, along with the rest of the form:

 

	$date = ($_POST['date']);
	$class = ($_POST['class']);
	$topic = ($_POST['topic']);
	$essques = ($_POST['essques']);
	$summary = ($_POST['summary']);
	$strat = ($_POST['strat']);
	$procedure = ($_POST['procedure']);
	$eval = ($_POST['eval']);
	$resources = ($_POST['resources']);
	$notes = ($_POST['notes']);
	$unit = ($_POST['unit']);

$sql = "INSERT INTO plan VALUES ('',  '".$date."', '".($class ? implode("," , $class) : "")."', '".addslashes($topic)."', '', '".addslashes($essques)."', '".addslashes($summary)."', '".($strat ? implode("," , $strat) : "")."', '".addslashes($procedure)."', '".($eval ? implode("," , $eval) : "")."', '".addslashes($resources)."', '".addslashes($notes)."', '".($unit ? implode("," , $unit) : "")."')";

 

Now--strat and eval work.  I get values in the database like 2,4,5 (which is how it's supposed to work).  For class and unit, though, I just get one number.  No commas or anything.

 

As far as I can see, I have set them up the EXACT same way, so why would two of them work and the other two not?  I don't understand what I've done wrong.

 

Are class and unit numeric values like strat and eval?

 

I'd start by checking input

echo '<pre>', print_r($_POST, 1), '</pre>';

 

and checking query string concatenation and content

echo $sql;

 

Any clues there?

 

EDIT: sorry, trigger-happy with post button. I meant to back out when I saw previous post.

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.