I want to update array data which comes in foreach loop. I am trying with script given below, it is only updating the very last row of data and giving error in this line -
if ($updateInvoiceAddedItems AND $updateInvoiceSubtractedItems)
.
PHP script
if (isset($_POST['submit'])) {
foreach($_POST['data'] as $key => $value) {
$invoiceItemId = intval($value['invoiceItemId']);
$itemId = intval($value['ItemId']);
$QTY = intval($value['QTY']);
$addedQTY = intval($value['addedQTY']);
$subtractedQty = intval($value['subtractedQty']);
$Total = is_numeric($value['total']) ? $value['total'] : false;
try {
if($addedQTY > 0) {
$updateInvoiceAddedItems = $db - > prepare("UPDATE `invoiceItems` SET
qty = qty + : addedQTY,
addedQty = addedQty + : addingQTY where id = : Iid ");
$updateInvoiceAddedItems - > execute(array(':Iid' => $itemId, ':addedQTY' => $addedQTY, ':addingQTY' => $addedQty));
}
elseif($subtractedQty > 0) {
$updateInvoiceSubtractedItems = $db - > prepare("UPDATE `invoiceItems` SET
qty = qty - : subtractedQty,
subtractedQty = subtractedQty + : subtractingQty where id = : iiId ");
$sub_invoiceItems - > execute(array(':iiId' => $itemId, ':subtractedQty' => $subtractedQty, ':subtractingQty' => $subtractedQty));
}
if ($updateInvoiceAddedItems AND $updateInvoiceSubtractedItems) {
echo ' <script> alert("success") </script>';
exit;
} else {
echo ' <script> alert("Error") </script>';
}
} catch (PDOException $e) {
echo 'Connection failed: '.$e - > getMessage();
}
}
}
Aucun commentaire:
Enregistrer un commentaire