【问题标题】:How to update the student semester after they pass the exam in one go? Already getting list of students一次通过考试后如何更新学生学期?已经获得学生名单
【发布时间】: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()。

标签: php html mysql


【解决方案1】:

我假设你使用了 POST 方法:

if(isset($_POST['submit'])){

    if(!empty($_POST['checkbox'])) {    
        foreach($_POST['checkbox'] as $value){
            echo "value : ".$value; // Here you get value of checked checkbox.
            // ----- Now Here Execute query to update Semester ------
        }
    }

}

【讨论】:

    【解决方案2】:

    好的,试试这个。您的想法是正确的,但我们将专门告诉查询要更新哪一行。否则查询只会为每一行添加一个学期。

    此外,您应该使用参数化查询。我不使用mysqli,所以我研究了它,我认为我拥有的代码是正确的。我还没有测试过。参数化查询将防止 sql 注入。

    试一试,它应该可以工作:

    if(isset($_POST["submit"])){
    
      if($_POST['update']){ //<--Check to make sure that update has values in it.
    
        for($i = 0; $i < count($_POST['update']); $i++){
    
                //Make sure the table name is correct.
                $query = "UPDATE students_in_course
    
                  SET 
                      semester = semester + 1
    
                  WHERE 
    
                    roll_number = ? ";
    
                //We are going to use paramterized query stucture.  This 
                //will prevent sql injection.    
                $prepare = $con->prepare($query);
                $prepare->bind_param('s', $_POST['update'][$i]);
                $prepare->execute();  
    
                if ($prepare->execute()) { 
    
                echo 'Student #' . $_POST['update'][$i] . ' was successfully updated. <br>';
    
                } else {
    
                  echo 'There was a problem inserting student # ' . $_POST['update'][$i] . ' into your database. <br>';
    
                 }
    
                $prepare->close();
    
            }
    
      }
    
    }
    

    上面的方法循环遍历每个被检查的学生。在每次迭代中,它都会对那个学生进行单独的查询。

    一种更高级的方法是直接使用 $_POST['update'] 数组进行一次查询,以使用 WHERE IN 子句扩展查询。这将更新仅使用一个查询选择的所有学生。

    像这样:

    if(isset($_POST["submit"])){
    
      if($_POST['update']){ //<--Check to make sure that update has values in it.
    
          $clause = implode(',', array_fill(0, count($_POST['update']), '?')); //create 3 question marks        
          //Make sure the table name is correct.
          $query = "UPDATE students_in_course
    
            SET 
    
            semester = semester + 1
    
            WHERE 
    
            roll_number IN ($clause) ";
    
            $types = str_repeat('s', count($_POST['update'])); //create 3 "s" or for bind_param.
    
            $prepare = $con->prepare($query);
            $prepare->bind_param($types, ...$_POST['update']);
            $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();
    
      }
    
    }
    

    我使用这个页面来研究用 mysqli 准备的语句。此页面上有大量信息和示例。你一定要看看它。

    https://websitebeaver.com/prepared-statements-in-php-mysqli-to-prevent-sql-injection

    【讨论】:

    • @Sandeep 我犯了一个错误。我已经修复了它。当前代码是最新的。
    • ...不可避免。
    • @Joseph_J 我非常感谢您为应对我所面临的困境所做的努力。多亏了您的帮助,我能够在复选框的帮助下将昏倒的学生提升到下学期。唯一的事情是他们通过增加 2 而不是 1 来提升。例如第 1 学期的学生被提升为 sem 3 而不是 sem 2。我已经用当前代码更新了代码。
    • @Sandeep 很高兴你取得了一些进展。您仍然遇到问题的原因是您正在使用的查询仍在循环中。删除 for 循环,我觉得它会工作得很好。请记住,查询旨在通过一个查询更新所有学生。如果它循环,它将为循环的每次迭代更新查询。干杯。
    • @Joseph_J 非常感谢您提供的所有帮助。你是救世主。在过去的几天里,我一直在努力解决这个问题。既然事情都解决了,我就放心了。再次,非常感谢。你是摇滚明星!
    猜你喜欢
    • 2011-03-20
    • 2023-04-08
    • 1970-01-01
    • 2017-08-27
    • 1970-01-01
    • 2021-02-17
    • 1970-01-01
    • 2021-08-01
    • 1970-01-01
    相关资源
    最近更新 更多