如何在一次通过考试后更新学生学期?已经获得学生名单

时间:2018-04-14 04:10:45

标签: php html mysql

更新:我希望在复选框的帮助下,将已故的学生提升到下一学期。对于那些检查过的学生,我希望更新学期。但学生学期增加2而不是1。从sem 1传出的学生被提升为sem 3而不是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>

2 个答案:

答案 0 :(得分:0)

我假设您使用了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 ------
        }
    }

}

答案 1 :(得分:0)

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

此外,您应该使用参数化查询。我不使用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