【发布时间】:2018-04-14 04:10:45
【问题描述】:
更新:我希望在复选框的帮助下将昏倒的学生提升到下学期。对于那些检查过的学生,我希望更新学期。但是学生学期增加了 2 而不是 1。例如sem 1 的昏倒学生被提升为 sem 3 而不是 sem 2。
这是我正在使用的更新代码:
$con=mysqli_connect("$host", "$username", "$password", "$db_name");
if(mysqli_connect_errno($con))
{
echo 'Failed to connect';
}
else
{ echo 'Connection established';
echo "<BR><BR>";
}
if(isset($_POST["submit"])){
//echo 'hello submit<br>';
if($_POST['checkbox']){
//echo 'hello checkbox<br>';
for($i = 0; $i < count($_POST['checkbox']); $i++)
{
echo count($_POST['checkbox']);
echo '<br>';
$temp = implode(',', array_fill(0, count($_POST['checkbox']),'?'));
echo $temp;
echo '<br>';
$query = "UPDATE students_in_courses
SET
semester = semester + 1
WHERE
roll_number IN ($temp) ";
$types = str_repeat('s', count($_POST['checkbox']));
$prepare = $con-> prepare($query);
$prepare-> bind_param($types, ...$_POST['checkbox']);
$prepare->execute();
if ($prepare->execute()) {
echo 'The student\'s records have been updated.';
} else {
echo 'There was a problem updating the student\'s records. <br>';
}
$prepare->close();
}
}
}
$sql="SELECT * FROM students_in_courses where course_name = 'B.Ed' ";
$result=mysqli_query($con,$sql);
if (mysqli_num_rows($result)==0)
{
echo 'No records found';
}
else
{
echo '<form name="frmactive" method="post" action="">';
echo '<table>';
echo '<tr><td colspan="4"><strong>Update multiple rows in mysql with checkbox</strong></td></tr>';
echo '<tr><td></td><td><strong>Roll Number</strong></td>';
echo '<td><strong>Course</strong></td>';
echo '<td><strong>Semester</strong></td>';
echo '<td><strong>Year</strong></td></tr>';
while($rows = mysqli_fetch_array($result))
{
echo '<tr><td><input name="checkbox[]" type="checkbox" id="checkbox[]" value = "'.$rows[0].'"></td>';
echo '<td>'.$rows[0].'</td>';
echo '<td>'.$rows[1].'</td>';
echo '<td>'.$rows[2].'</td>';
echo '<td>'.$rows[3].'</td></tr>';
}
echo '<tr><td><input type = "submit" name = "submit" value = "submit">';
echo '</table></form>';
}
?>
<html>
<body>
</body>
</html>
【问题讨论】:
-
请向我们提供一些代码示例,以便我们查看您已经尝试过的内容。另外,请更具体地说明您要实现的目标。
-
嗨@Joseph_J 我已经添加了到目前为止我尝试过的代码。我希望通过单击复选框将一批通过考试的学生推广到下学期。我已经能够获得学生名单,但无法更新学期。在这方面的任何帮助将不胜感激,因为我的作业将于周二到期。
-
卷号是否唯一
-
是的。卷号是唯一的。
-
嗯,您的代码完全容易受到 SQL 注入攻击,以及黑客的脚本注入/XSS 攻击,在被黑客攻击之前,您可能应该查看 htmlentities() 和 mysqli_read_escape_string()。