问题
所以有一些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代码
$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
答案 0 :(得分:0)
最简单的方法是在输入字段中使用数组名称。这样,您的$_POST
将出现在一堆易于循环的分组数组中:
<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
?>
<tr>
<td><input type='text' name='student[<?php echo $studentID ?>][firstname]' value='<?php echo $firstname ?>' /></td>
<td><input type='text' name='student[<?php echo $studentID ?>][lastname]' value='<?php echo $lastname ?>' /></td>
<td><input type='text' name='student[<?php echo $studentID ?>][teacherFirst]' value='<?php echo $teacherFirstname ?>' /></td>
<td><input type='text' name='student[<?php echo $studentID ?>][teacherLast]' value='<?php echo $teacherLastname ?>' /></td>
</tr>
<?php
}
// add to row
$value += 1;
}
?>
</table>
<button type="submit" name="update">Update</button>
</form>
处理$_POST
时,循环遍历$_POST['student']
数组,键将是id,其余数据包含在数组中。