I want to fetch array data from "orders" Table WHERE order_id =2 and insert into "inventory_log" Table. but my code is only inserting the last row
orders TABLE
order_id product_id quantity
2 1 9
2 3 2
3 6 3
3 5 2
2 7 1
I want this:
inventory_log TABLE
id product_id quantity
1 1 9
2 3 2
3 7 1
Find my code below:
<?php
define('DB_SERVER','localhost');
define('DB_USER','root');
define('DB_PASS' ,'');
define('DB_NAME', 'store');
$con = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Insert into inventory_log TABLE
if(isset($_POST['Submit']))
{
$productID=$_POST['product_id'];
$IssueQty=$_POST['quantity'];
$inventory=mysqli_query($con,"insert into inventory_log(productID, IssueQty) values('$productID', '$IssueQty')");
echo '<script>alert("Sucessful")</script>';
}
?>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title></title>
</head>
<body>
<form method="post" name="submit">
<table border="1">
<thead>
<tr>
<th>Product ID</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
<?php
// Get order items from orders TABLE
$result = $con->query("SELECT order_id, product_id, quantity
FROM orders
WHERE order_id = 2");
if($result->num_rows > 0){
while($item = $result->fetch_assoc()){
$product_id = $item["product_id"];
$quantity = $item["quantity"];
?>
<tr>
<td><input type="text" name="product_id" value="<?php echo $product_id; ?>"></td>
<td><input type="text" name="quantity" value="<?php echo $quantity; ?>"></td>
</tr>
<?php } } ?>
</tbody>
</table>
<button type="submit" name="Submit" class="btn btn-success btn-sm">Submit</button>
<form>
</body>
</html>