我已被示出在下面foreach循環插入所有的複選框(選中和未選中)
<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 HTML複選框形式複選框正在插入,而不僅僅是已被選中的複選框。我一直在嘗試一系列的事情,似乎沒有任何工作。這是處理程序。
<?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');
}
}?>
爲什麼只設置三個不同的變量使用最後一個? '$ university_department = $ row ['university_department'];' '$ _SESSION ['university_department'] = $ university_department;' '$ universityDepartment = $ _SESSION ['university_department'];' – Pete 2013-03-15 14:47:05
只是一個小提示:當你通過'include'或'require'包含文件時,你應該使用'include_once()'或'require_once()',以便在包含包含其他文件的路上,你不會被定義爲函數2次或導致更長的加載時間。 – Mic1780 2013-03-15 14:58:11