lundi 30 mars 2015

The Array is working but do not insert random values to table

I have courses to let user select from, I take SET value(drop down list) and checkbox value and insert them into table. Now when I print_r the array after selection is working fine. When I check the table I find checkbox value is correctly inserted, but the value of the drop down list is always 0 unless I start to select the drop down list(SET VALUE) from the beginning it will work fine??? I know sound complicated,but simply I can not insert array values of the drop down list(SET VALUE) randomly I need to select from the first value to make it work??! Any help please!



<?php include("includes/db_connection.php"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ift.tt/kkyg93">
<html xmlns="http://ift.tt/lH0Osb">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<style type="text/css">
<!--
body {
background-color: #E6E6FA;
}
-->
</style>


</head>

<body>
<form id="form" name="form" method="post" action="">
<table width="1047" border="1">
<tr>
<th colspan="8" bgcolor="#009966" scope="row">&nbsp;</th>
</tr>
<tr>
<th width="77" scope="row">set</th>
<th width="77" scope="row"><div align="center"><strong>Select All

</strong></div></th>
<td width="107"><div align="center"><strong>Course Code </strong></div></td>
<td width="130"><div align="center"><strong>Course Name </strong></div></td>
<td width="50"><div align="center"><strong>Credits</strong></div></td>
<td width="162"><div align="center"><strong>Prerequisite Courses</strong></div></td>
<td width="196"><div align="center"><strong>Semester Available</strong></div></td>
<td width="196"><div align="center"><strong>Set Number </strong></div></td>
</tr>
<?php

if(isset($_POST['Submit']) AND $_POST['Submit'] == 'Submit')
{


print_r($_POST);
$coursid =isset($_POST['checkbox']) ? $_POST['checkbox'] : '';
$curid=isset($_GET['id']) ? $_GET['id'] : '';
$setnumber=isset($_POST['select']) ? $_POST['select'] : '';

for($x = 0; $x < count($coursid); $x++)
{
if ($setnumber[$x] == "" || strlen($setnumber[$x]) == 0)
continue;
$sql2=$mysqli->query("INSERT INTO `curriculumcourses`(`course_id`, `curriculum_id`, `set_number`) VALUES ( '".$coursid[$x] ."','". $curid ."','". $setnumber[$x] ."')");
}
}


?>

<?php
$sql1 = "SELECT * FROM courses";
$result = $mysqli->query($sql1);

if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
?>
<tr>
<th scope="row"> <label>
<select name="select[]" id="select[]">
<option value="0">NO SET</option>
<option value="1">SET 1</option>
<option value="2">SET 2</option>
<option value="3">SET 3</option>
<option value="4">SET 4</option>
<option value="5">SET 5</option>
</select>
</label></th>
<th height="27" scope="row"><label>
<input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $row['course_id']; ?>" />
</label></th>
<td><div align="center"><?php echo $row["course_code"];?></div></td>
<td><div align="center"><a href="add_prerequisites.php?id=<?php echo $row['course_id'];?>"/a><?php echo $row["course_name"];?></div></td>
<td><div align="center"><?php echo $row["credits"];?></div></td>
<td><div align="center">

<?php
$cid=$row['course_id'];
$sql3 = "SELECT * FROM prerequisites WHERE cours_id='$cid'";
$result3 = $mysqli->query($sql3);

if ($result3->num_rows > 0) {
while($row3 = $result3->fetch_assoc()) {

$s= $row3['set'];
if ($s==0){

echo $row3['prereq_id'];
}
else if($s==1){
echo $row3['prereq_id'];

}
else if($s==2){
echo $row3['prereq_id']." "."OR"." ";

}else if($s==3){
echo " "."AND"." ".$row3['prereq_id'];
}
}
}
?>


</div></td>
<td><div align="center"><?php $sems= $row["semester_ava"];
if ($sems==0){
echo "Fall ";
}
else if($sems==1){
echo "Spring";
}
else if($sems==2){
echo "BOTH";
}



?></div></td>
<td><div align="center">

</div></td>
</tr>

<?php
}
}
?>

<tr>
<th colspan="8" bgcolor="#009966" scope="row"><label>
<input type="submit" name="Submit" value="Submit" />
</label></th>
</tr>
</table>


<p>&nbsp;</p>
<p>&nbsp;</p>
</form>
</body>
</html>

Aucun commentaire:

Enregistrer un commentaire