php - 一次更新多个等级

标签 php mysql mysqli

我想更改教师列表中每个学生的成绩。 我不知道这里出了什么问题。 请你能帮帮我吗? 这是sub-doc.php的代码

这是savesub_doc.php

 <?php
        if(isset($_POST["submit"])) {

       foreach($_POST['sh_grade'] as $gr){

           $i =$_POST['sh_id'];
           $sqiil="UPDATE grade SET sh_grade[]='$gr' WHERE sh_id='$i' ";

                  if(mysqli_query($conn,$sqiil)){
                              }                                   

            }}
 ?>

子文档.php

 <input type="hidden" name="sh_id[]" value="<?php echo $row["sh_id"]; ?>" >            

 <input type="text" name="sh_grade[]" value='<?php echo $row['sh_grade'];?>">
 <input type="submit"  value="Submit" name="submit"> 

最佳答案

您应该使用参数化查询来防止 SQL 注入(inject)。而且我怀疑表列的名称中是否包含 []

您还需要索引$_POST['sh_id']以获得每个年级对应的ID。

if (isset($_POST['submit'])) {
    $stmt = mysqli_prepare($conn, "UPDATE grade SET sh_grade = ? WHERE sh_id = ?");
    mysqli_stmt_bind_param($stmt, "si", $gr, $id);
    foreach ($_POST['sh_grade'] as $i => $gr) {
        $id = $_POST['sh_id'][$i];
        mysqli_stmt_execute($stmt);
    }
}

关于php - 一次更新多个等级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43291792/

相关文章:

php - MySql:更新多个列

php - 错误: "Activation Server cant create object" in IE

mysql - 将 WordPress 站点移动到新服务器相同的 URL

php - Laravel Eloquent - 日期属性未作为 Carbon 对象检索

php - 设置时区未优化

php - 如何使用MySQLi更新BBDD。变量应该通过引用传递?

php - 为什么我在 PHP App 和 PhpMyAdmin 上得到不同的查询结果?进行严格/正常搜索的正确方法是什么?

php - 对mysql中多个用户拥有的记录进行计算

mysql - 主键和自动增量: it is possible to increment not even the same value for all the tables?

php - 在 MySQLi 准备好的语句中,STMT 代表什么?