簡體   English   中英

如何在php和mysql中使用數組和for循環更新表列?

[英]How to update table column using array and for loop in php and mysql?

for ($key=0; $key < count($_POST['marks']); $key++) {

            $from_marks = $_POST['from'][$key];
            $get_marks = $_POST['marks'][$key];

            //echo $from_marks." ";
            if($get_marks > $from_marks){
                // header("location: ../../pages/marks.php?over=err");
                // break;

                echo "Cant add more marks <br/>";

            }
            else{
                echo $get_marks."<br/>";

                $update_marks_query = $db->prepare(
                    "UPDATE sc_marks SET get_marks='"
                    .$get_marks
                    ."' WHERE _sid='$sc_foreign_id' AND exam_type='$select_exam_type' ");
                $update_marks_query -> execute();
            }
}

當我執行代碼時出現問題,我在表的每一行中獲得了最后一個獲取的值。

更新后的數據結果:

更新后的數據結果

    <?php
    include "./connection/config.php";

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

        $sc_foreign_id = $_POST['sc_foreign_id'];
        $select_exam_type = $_POST['select_exam_type'];

        for($key=0; $key<count($_POST['marks']); $key++){

            $from_marks = $_POST['from'][$key];
            $get_marks = $_POST['marks'][$key];

            echo $from_marks." ";


            if($get_marks > $from_marks){
                // header("location: ../../pages/marks.php?over=err");
                // break;

                echo "Marks Vadhu Chhe <br/>";

            }
            else{
                echo $get_marks."<br/>";

                $update_marks_query = $db->query("UPDATE sc_marks SET get_marks='".$get_marks."' WHERE _sid='$sc_foreign_id' AND exam_type='$select_exam_type' ");
            }
            // else{
                // $update_marks_query = $db->prepare("UPDATE sc_marks SET get_marks='$get_marks' WHERE _sid='$sc_foreign_id' ");
                // $update_done = $update_marks_query -> execute();
            // }
        }

        // if($update_done){
            // echo "Successfully Updated";
            // header("location: ../../pages/marks.php?add-marks=yes");
        // }
        // else{
            // echo "Error";
            // header("location: ../../pages/marks.php?add-marks=error");
        // }
    }
?>

我建議你在for循環之前准備你的更新語句

$query = $db->prepare("UPDATE sc_marks SET get_marks=? WHERE _sid=? AND exam_type=?");

for ($key=0; $key < count($_POST['marks']); $key++) {

            $from_marks = $_POST['from'][$key]; //add some validation here
            $get_marks = $_POST['marks'][$key]; //e.G with regex

            //echo $from_marks." ";
            if($get_marks > $from_marks){
                // header("location: ../../pages/marks.php?over=err");
                // break;

                echo "Cant add more marks <br/>";

            }
            else{
                echo $get_marks."<br/>";

                $query->execute($get_marks, $sc_foreign_id, $select_exam_type); 

            }
}

//Then attach the parameters during each iteration within the loop

您目前的方法存在安全風險,除了效率低於預期。 閱讀SQL注入。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM