Home > front end >  How do I update each row in a column to each array value, instead of each row updating to the last a
How do I update each row in a column to each array value, instead of each row updating to the last a

Time:01-31

How do I update each row in the column to each array value, instead of each row updating to the last array value?

        foreach ($array as $i => $value) {
            $sql = "UPDATE table SET column_1='$value' WHERE column_2='value2'";
            mysqli_query($connDB, $sql);
        }

Input:

123
456
789

$array:

[0] => 123
[1] => 456
[2] => 789

Current result:

column_1 column_2
789 value2
789 value2
789 value2

What I want:

column_1 column_2
123 value2
456 value2
789 value2

CodePudding user response:

You set $value1 in the query, change it to $value:

$sql = "UPDATE table SET column_1='$value' WHERE column_2='value2'";

         

CodePudding user response:

the problem is column_2 is not unique for each row. you must have unique value like id in a table for proper updating or deleting so you can select id of all rows with condition column_2=value2 and then update each row using its unique id

  •  Tags:  
  • Related