So I need to run my sql query in a foreach loop, but, say there are two variables in the foreach loop, the query only executes the iteration with the first variable twice, instead of executing both the first and the second variable.
My code
$sql = "SELECT * FROM users WHERE idUsers = '$selected';";
$result = mysqli_query($conn, $sql);
if($row = mysqli_fetch_assoc($result))
{
foreach($_POST['order-check'] as $check)
{
$sql2 = "UPDATE order_table SET order_status = 'Processing', assigned_vendor = '$selectedvendor' WHERE order_id = '$check';";
$result2 = mysqli_query($conn, $sql2);
exit();
}
}
else{
echo "failed";
exit();
}
Here, $selected is a POST variable from another page
exit()in there, so it will only run the loop once. You also only fetch one row in your select query, while you loop the$_POST['order-check']until the end (except youexitafter the first iteration).$row. Why are you doing it in the loop?if($result)?while ($row = mysqli_fetch_assoc($result)).