5
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();
            }
}

The issue is occurred when I execute the code, I got the last fetched value in each rows of the table.

Data result after update:

Data result after update

2
  • 1
    The fact that all your rows end up containing the same data would suggest that the WHERE clause of your UPDATE statement always matches every row in the table. I can't really give a more accurate answer without knowing where and how the used variables $sc_foreign_id and $select_exam_type are defined, and what kind of data their associated fields contain in the database. Commented Oct 16, 2015 at 12:39
  • By the way, you should prepare your update statement before the for loop $query = $db->prepare("UPDATE sc_marks SET get_marks=? WHERE _sid=? AND exam_type=?"); and then attach the parameters during each iteration with $query->execute($get_marks, $sc_foreign_id, $select_exam_type);. Your current approach is a security risk, apart from being less efficient than it could be. Read about SQL injection. Commented Oct 16, 2015 at 12:45

2 Answers 2

5
    <?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");
        // }
    }
?>
Sign up to request clarification or add additional context in comments.

Comments

0

I would suggest you to prepare your update statement before the for loop

$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

Your current approach is a security risk, apart from being less efficient than it could be. Read about SQL injection.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.