0
因此,一個有一些PHP代碼,從數據庫,並輸出該信息獲取學生信息。該信息是在input
,使用戶輸出可以編輯信息。然後,當用戶完成編輯信息時,我需要獲取我擁有的數據。我的問題是,我不知道如何打破這些信息,因此很容易讓我改變在數據庫中的信息。
學生表
studentID | firstname | lastname | teacherID
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);
$value = 0; // counts rows
// loop through each student
foreach ($getStudent as $student) {
$studentID = $student['studentID'];
$firstname = $student['firstname'];
$lastname = $student['lastname'];
$teacherID = $student['teacherID'];
// get teacher
$getTeacher = $link->prepare("SELECT * FROM teachers
WHERE teacherID = :teacherID");
$getTeacher->execute(array(
"teacherID" => $teacherID
));
$getTeacher = $getTeacher->fetchAll();
// loop through each teacher
foreach ($getTeacher as $teacher) {
$teacherFirstname = $teacher['firstname'];
$teacherLastname = $teacher['lastname'];
// output data
echo "
<tr>
<td><input type='text' name='studentID-$value' value='$studentID'></td>
<td><input type='text' name='firstname-$value' value='$firstname'></td>
<td><input type='text' name='lastname-$value' value='$lastname'></td>
<td><input type='text' name='teacherFirst-$value' value='$teacherFirstname'></td>
<td><input type='text' name='teacherLast-$value' value='$teacherLastname'></td>
</tr>
";
}
// add to row
$value += 1;
}
?>
</table>
<button type="submit" name="update">Update</button>
</form>
PHP代碼,改變了DB代碼
$dataCount = 0;
foreach ($_POST as $data) {
if($dataCount % 5 === 0) {
echo "<br><br>";
echo $data . ", ";
} else {
echo $data . ", ";
}
$dataCount++;
}
什麼形式循環,如屏幕
StudentID Firstname Lastname Teacher's Firstname Teacher's Lastname
_______________________________________________________________________________________
1 Bill Roy Jonathan Kung
2 Travis Roy Shanin Harnik
你的問題是混亂的。 $ _POST變量不是包含數據的數組嗎? – styl3r
但我不知道如何打破這些信息,因此很容易讓我改變在數據庫中的信息。 –
' 「UPDATE學生SET姓= '{$ DB-> real_escape_query($ _ POST [' FIRSTNAME '])' WHERE StudentID = '{$ _ POST [' StudentID ']}'」' – styl3r