数组正在运行但不向表中插入随机值

时间:2015-03-29 05:36:37

标签: php html forms

我有让用户选择的课程,我拿SET值(下拉列表)和复选框值并将它们插入表中。现在当我在选择后print_r数组工作正常。当我检查表格时,我发现正确插入了复选框值,但下拉列表的值始终为0,除非我从头开始选择下拉列表(SET VALUE)它会正常工作???我知道声音很复杂,但我只是无法随机插入下拉列表(SET VALUE)的数组值我需要从第一个值中选择以使其工作??!请帮忙!

<?php include("includes/db_connection.php"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<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>

1 个答案:

答案 0 :(得分:0)

我想出来了,我刚刚添加了默认选择值

<option selected disabled>Please Select</option>