I am trying to update a SQL table from a PHP array using foreach() . I noticed that only the last array value is written to all table fields. I went over many examples here but none of them solved it to me. eg (Simple update sql table from PHP array and some others)
here is the array $product_qttys:
Array ( [qty0] => 1 [qty1] => 4 [qty2] => 2 [qty3] => 3 [qty4] => 6 )
and here is the code:
foreach ($product_qttys as $key => $pr_qtys){
$qttys = mysqli_real_escape_string($con, $pr_qtys);
// print_r ($qttys);
$sql = "UPDATE table-a SET qty = '$qttys' WHERE ip_add = '$ip'";
$result = mysqli_query($con,$sql);
}
echo "<br/>check SQL table<br/>";
$sqla = "SELECT * FROM table-a WHERE ip_add='$ip'";
$querya = mysqli_query($con,$sqla);
if (mysqli_num_rows($querya) > 0) {
while ($producta = mysqli_fetch_assoc($querya)) {
echo ("item_qty=" . ($item_qty = $producta ['qty']) . "<br/>");
}
}
This is the output i am getting:
check SQL table
item_qty=6
item_qty=6
item_qty=6
item_qty=6
item_qty=6
PhpMyAdmin table is also showing the same. did I miss something or had sth wrong? Appreciate your feedback/input & hoping to close it. thank you.