My problem is as follows.. I want to add multiple items to a table at the same time. This is a inventory system and according to a purchase order, items will be added. I want to update 3 tables simultaneously. The first table relates to invoice data and the second table relates to received item details. The third table relates to current stock.
If an item which I am going to add to current stock table already exists it should be updated and if it does not exist it should be added to the current stock table. I use dynamically generated text box to add items to table. But I'm unable to execute ON DUPLICATE UPDATE KEY
query in the for loop. My code is as follows..
if (is_array($qty) && ($item)) {
for ($i = 0; $i < sizeof($qty); $i++) {
$query2 = "INSERT INTO received_items (po_id,invoice_number,item_name,qty)VALUES($id,'$invoice',$item[$i]',$qty[$i])";
$query3="INSERT INTO current_stock (item-name,Qty) VALUES('$item[$i]',$qty[$i]) ON DUPLICATE KEY UPDATE item-name='$item[$i]',qty=qty+$qty[$i]";
$result_1 = mysqli_query($conn, $query2);
$result_2 = mysqli_query($conn, $query3);
if (($result_1)&&($result_2)) {
echo '<script type="text/javascript">';
echo 'alert("Sucessfully Updated your Details");';
echo 'window.location = "../htdocs/dashbd.php";';
echo '</script>';
}else{
echo '<script type="text/javascript">';
echo 'alert("Failed to Update Your details.Try Again");';
echo 'window.location = "../htdocs/received_item.php";';
echo '</script>';
}
}
}