I have got the following code:
if (count($sales) > 0) {
foreach ($sales as $sale) {
//Generate variables
$user_id = $_SESSION['user_id'];
$sales_id = $sale['sales_id'];
$product = $sale['product'];
$quantity = $sale['quantity'];
$default_printed = 0;
$datetime = $_SESSION['session_date']." ".date('G:i:s');
echo $sales_id.", ".$product.", ".$quantity.", ".$user_id."<br />";
if ($stmt = $mysqli->prepare("INSERT INTO sales (uid,product,printed,saledatetime,jpgID,quantity) VALUES (?,?,?,?,?,?)")) {
$stmt->bind_param('isisii',$user_id,$product,$default_printed,$datetime,$sales_id,$quantity);
$stmt->execute();
$stmt->close();
}
}
}
It should work, but for some reason it only adds to the database the FIRST time it runs... the echo produces the following, just to prove that the array is actually being looped through.
83, KR, 2, 2
84, KR, 1, 2
84, LR, 1, 2
85, KR, 1, 2
86, LR, 2, 2
87, KR, 1, 2
87, LR, 3, 2
89, KR, 2, 2
Why are the values not being added to the database on subsequent runs?