我有一个使用 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>
现在,当我使用 myuniversity_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');
}
}?>