php - foreach 循环插入所有复选框(选中和未选中)

标签 php html mysql loops foreach

我有一个使用 php 生成 html 复选框的表单,如下所示

<p><form name="university" action="/university_handler" method="post">
  <fieldset>
    <table class="table">
      <thead>
        <tr>
          <th><span class="help-block">University Department</span></th>                            
        </tr>
      </thead>
      <tbody>  
        <tr> 
          <td><?php 
            $query = mysqli_query($db, "SELECT university_department FROM university WHERE university_id = '$university_id'") 
                or die  ("Could not search!");
            while($row = mysqli_fetch_array($query)){
              $university_department = $row['university_department'];
              $_SESSION['university_department'] = $university_department;
              $universityDepartment = $_SESSION['university_department'];
              echo "<label><input type='checkbox' name='university_department[]' value='{$universityDepartment}'>$universityDepartment</label><br><input type='text' value='' name='professor_name[{$universityDepartment}]' placeholder='Professor-Name'><input type='text' value='' name='class_name[{$universityDepartment}]' placeholder='Class-Name'>";}
          ?></td>
        </tr>
      </tbody>
    </table> 
    <button type="submit" name="Submit"class="btn btn-info">Submit</button>
  </fieldset>
</form></p>

现在,当我使用 university_handler 将值插入数据库时​​,所有复选框都会插入,而不仅仅是已选中的复选框。我一直在尝试一系列的事情,但似乎没有任何效果。这是处理程序。

<?php
session_start();
include("connect.php");

$university_id = $_SESSION['university_id'];

// check if share_form is submitted and not empty
$error_message = "";
if(is_array($_POST['university_department']) && !empty($_POST['university_department'])){
  $error = array();
  $universityDepartment = $_POST['university_department'];
  if (count($universityDepartment)>0){
    foreach (str_replace('#', '', $_POST['class_name']) as $departmentName => $stripid){
      $class_name_backslash = $stripid . '/';
      $class_name = mysqli_real_escape_string($db, $stripid);
      print_r($class_name);
    }
    $query_uni = ("INSERT INTO temp_list(departmentName, class_name, professor_name) VALUE ('$departmentName','$class_name', '$professor_name')");
    $q_u = mysqli_query($db, $query_uni) or die ('Error posting data');
}
}?>

最佳答案

我喜欢@Martin 的回答。我唯一想改变的是 $_REQUEST

当使用$_REQUEST时,你是在说嘿看看帖子或获取变量并使用其中一个值。那么,如果您同时拥有同名的 $_POST$_GET 变量,会发生什么情况呢?一个将被使用,而另一个则不会。

所以让我们以不同的方式使用相同的代码。

$checkBoxName = (isset($_POST['checkBoxName']) ? $_POST['checkBoxName'] : (isset($_GET['checkBoxName']) ? $_GET['checkBoxName'] : ''));
if ($checkBoxName != '') {
    //do stuff here
}

这样一来,如果不使用 Google Chrome 开发者工具之类的工具,您就无法查看帖子信息,因此最好按此顺序执行操作,以便您先检查未看到的内容,然后再查看可以看到的内容。

希望这有帮助 =)

编辑:

根据您提供给我的信息,我想出了一种仅插入您勾选的类的方法。请随意进行更改,但这应该有效

<?php
$departList =   ($_POST['university_department'] ? $_POST['university_department'] : ($_GET['university_department'] ? $_GET['university_department'] : array()));
$classList =    ($_POST['class_name'] ? $_POST['class_name'] : ($_GET['class_name'] ? $_GET['class_name'] : array()));
$profList = ($_POST['professor_name'] ? $_POST['professor_name'] : ($_GET['professor_name'] ? $_GET['professor_name'] : array()));
if (count($departList) > 0) {
    foreach ($departList as $key => $val) {
        $class =    $classList[$val];
        $professor =    $profList[$val];
        $query_uni = ("INSERT INTO temp_list(departmentName, class_name, professor_name) VALUE ('$val','$class', '$professor')");
        $q_u = mysqli_query($db, $query_uni) or die ('Error posting data');
    }
}

?>

祝你好运=)

关于php - foreach 循环插入所有复选框(选中和未选中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15435198/

相关文章:

mysql - 错误代理 : Redirection of MySQL queries

mysql - 更新组 : UPDATE or DELETE + INSERT 中的用户列表

php - 在 SELECT 语句中插入变量

jquery - 动态 HTML 表单

html - CSS 子级悬停、旋转和剪辑 : Pie Chart Issues

html - 连续删除列表

php - 为什么 php 没有运行?

php - 来自有序查询的Mysql查询

php - 将文本分成两半,但在最近的句子处

mysql - 如何为 If Exists Update Else Insert 编写 MySQL 查询?