【问题标题】:The Array is working but do not insert random values to table数组正在工作,但不向表中插入随机值
【发布时间】:2015-03-29 05:36:37
【问题描述】:

我有课程让用户从中选择,我将 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>

【问题讨论】:

  • 老实说,我完全不明白你在说什么。

标签: php html forms


【解决方案1】:

我想通了,我只是添加了默认选择值

<option selected disabled>Please Select</option>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-25
    • 1970-01-01
    • 2014-07-21
    • 1970-01-01
    • 1970-01-01
    • 2016-02-24
    相关资源
    最近更新 更多