I’m trying to do a bulk insert here using Array. But i am unable to get the user_id
to save it into each row of the product that is added into the table using bulk insert. Can i know what is the way for me to insert my user_id
from the <select>
element and save it into the table called cart
?
This is how my cart table looks like after i successfully POST the items selected into the cart
table. The user_id
selected will only be saved into the first selected item.
<div class="col-12">
<div class="card">
<div class="card-body">
<h4 class="card-title"> Step 2: Insert Selected Product Values</h4>
<div class="table-responsive" style="max-height: 60vh">
<div>
<?php if(isset($message)) { echo $message; } ?></div>
<!--<form method="post" action="">-->
<table class="table">
<thead class="text-primary">
<th style=" position: sticky;top: 0; background: white";>
Product
</th>
<th style=" position: sticky;top: 0; background: white";>
Name
</th>
<th style=" position: sticky;top: 0; background: white";>
Avaliable Stock
</th>
<th style=" position: sticky;top: 0; background: white";>
Item Price(RM)
</th>
<th style=" position: sticky;top: 0; background: white";>
Insert Product Quantity
</th>
</thead>
<tbody id="products">
<form action="includes/add-item-to-cart-invoice.php" method="POST">
<?php
$product_id = $_REQUEST['id'];
$ids = explode(",",$product_id);
$ids = array_splice($ids, 0);
foreach($ids as $product_id){
$sql = "SELECT *, products.id as product_id FROM products
LEFT JOIN sellers ON products.seller_id = sellers.id
WHERE products.id = '".$product_id."'
ORDER BY product_created DESC ";
$query = $conn->query($sql);
{
while ($row = $query->fetch_assoc()) {
$max = $row['product_stock'];
?>
<tr>
<td>
<img src="https:/mywebsite/<?php echo !empty($row['product_photo']) ? $row['product_photo'] : ''; ?>" style="height: 50px; width: 50px;">
</td>
<td>
<?php echo $row['product_title']; ?>
</td>
<td>
<?php echo $row['product_stock']; ?> <?php echo $row['product_unit']; ?>
</td>
<td>
<div class="col-md-6">
<input type="number" name="cart_price[]" step=".01" class="form-control text-center" required>
</div>
</td>
<td>
<div class="col-md-6">
<input type="number" name="cart_qty[]" step=".01" class="form-control text-center" required>
</div>
</td>
<td>
<div class="col-md-12">
<input type="hidden" name="cart_name[]" value="<?php echo $row['product_title']; ?>">
<input type="hidden" name="cn_cart_name[]" value="<?php echo $row['cn_product_title']; ?>">
<input type="hidden" name="m_cart_name[]" value="<?php echo $row['m_product_title']; ?>">
<input type="hidden" name="cart_image[]" value="<?php echo $row['product_photo']; ?>">
<input type="hidden" name="cart_unit[]" value="<?php echo $row['product_unit']; ?>">
<input type="hidden" name="product_id[]" value="<?php echo $row['product_id']; ?>">
<input type="hidden" name="seller_id[]" value="<?php echo $row['seller_id']; ?>">
</div>
</td>
</div>
<?php
}
}
}
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<div class="col-12">
<div class="card">
<div class="card-body">
<h4 class="card-title"> Step 3: Select User</h4>
<div class="col-md-12">
<div class="form-group">
<label>SELECT SELLER *</label>
<select class="form-control" name="user_id[]" required>
<option value="">Select Users</option>
<?php
$ssql = "SELECT * FROM users ";
$squery = $conn->query($ssql);
while ($srow = $squery->fetch_assoc()) {
?>
<option value="<?php echo $srow['id'];?>" ><?php echo $srow['user_fullname'];?></option>
<?php } ?>
</select>
</div>
</div>
<div class="d-flex p-4 justify-content-around">
<div class="row">
<button type="button" name="btn_delete" id="btn_previous" value="True" class="btn btn-danger btn-icon-split m-2" onclick="window.location='add-invoice.php'">
<span class="icon text-white-50">
</span>
<i class="fa fa-arrow-left" aria-hidden="true"></i>
<span class="text">Back</span>
</button>
<button type="submit" name="add_invoice_details" id="btn_next" value="True" class="btn btn-info btn-icon-split m-2">
<span class="icon text-white-50">
</span>
<i class="fa fa-arrow-right" aria-hidden="true"></i>
<span class="text">Next</span>
</button>
</div>
</div>
</form>
</div>
</div>
</div>
This is my includes/add-item-to-cart-invoice.php
<?php
include('../session.php');
$data =$_POST;
echo"<pre>";
var_dump($data);
$count = count($_POST['product_id']);
foreach($_POST['product_id'] as $i => $value){
$sql = "INSERT INTO `cart`
(`cart_name`,`cn_cart_name`,`m_cart_name`, `cart_image`,
`cart_qty`, `cart_unit`, `cart_price`, `product_id`, `user_id`,`seller_id`)
VALUES
(
'{$_POST['cart_name'][$i]}',
'{$_POST['cn_cart_name'][$i]}',
'{$_POST['m_cart_name'][$i]}',
'{$_POST['cart_image'][$i]}',
'{$_POST['cart_qty'][$i]}',
'{$_POST['cart_unit'][$i]}',
'{$_POST['cart_price'][$i]}',
'{$_POST['product_id'][$i]}',
'{$_POST['user_id'][$i]}',
'{$_POST['seller_id'][$i]}'
)";
$conn->query($sql);
}
?>
Can i know what can i do so that the selected user_id
will be saved into each and every row of the item selected?