0
問題遍歷所有輸入值
所以我的問題是,我有去到數據庫並獲取學生數據的一些PHP代碼。然後該程序將學生信息輸出到屏幕上。信息放在input
內,因爲用戶將能夠編輯信息。當用戶編輯信息並點擊Update
時,我需要獲取有關數據庫更新的信息。我的問題是,我不知道如何循環所有輸入。
PHP代碼 - 當用戶點擊Update
foreach ($_POST as $data) {
echo $data . "<br>";
}
PHP代碼 - 允許用戶編輯
<form action="server/edit/students.php" method="post">
<table>
<tr>
<th>Student ID</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Teacher's Firstname</th>
<th>Teacher's Lastname</th>
</tr>
<?php
// get student info
$getStudent = $link->prepare("SELECT * FROM students");
$getStudent->execute();
$getStudent = $getStudent->fetchAll(PDO::FETCH_ASSOC);
foreach ($getStudent as $student) {
$studentID = $student['studentID'];
$firstname = $student['firstname'];
$lastname = $student['lastname'];
$teacherID = $student['teacherID'];
$getTeacher = $link->prepare("SELECT * FROM teachers
WHERE teacherID = :teacherID");
$getTeacher->execute(array(
"teacherID" => $teacherID
));
$getTeacher = $getTeacher->fetchAll();
foreach ($getTeacher as $teacher) {
$teacherFirstname = $teacher['firstname'];
$teacherLastname = $teacher['lastname'];
echo "
<tr>
<td><input type='text' value='$studentID'></td>
<td><input type='text' value='$firstname'></td>
<td><input type='text' value='$lastname'></td>
<td><input type='text' value='$teacherFirstname'></td>
<td><input type='text' value='$teacherLastname'></td>
</tr>
";
}
}
?>
</table>
<button type="submit" name="update">Update</button>
</form>
首先,我沒有看到任何形式的標籤。我想你只是忽略了那個?另一件事,你需要添加'name'-屬性到你的輸入字段,否則他們不會被髮布。 –
我編輯了我的問題 –
我還建議您在第一次查詢時使用'JOIN'來獲取教師,而不是在每次迭代中再次調用數據庫。 –