I have a product gallery in which products are displayed from the database. they have productID product_name product_price etc and a checkbox under the product detail on every product. the functionality I want is when I checked multiple products they should add to the selection table with a unique userID.
<form action="add_selection_script.php" method="POST" enctype="multipart/form-data">
<select class="form-select tw-capitalize" name="u_id">
<option value="">Select a Customer</option>
<?php
$get_users = "SELECT * FROM users";
$run_users = mysqli_query($con, $get_users);
while ($row_users = mysqli_fetch_array($run_users)) {
$u_id = $row_users['u_id'];
$username = $row_users['user_username'];
echo "<option value='$u_id'>$username</option>";
}
?>
</select>
<?php
$get_products = "SELECT * FROM products";
$run_products = mysqli_query($con, $get_products);
while ($row_products = mysqli_fetch_array($run_products)) {
$id = $row_products['id'];
$pro_id = $row_products['product_id'];
$pro_cat = $row_products['product_cat'];
$pro_title = $row_products['product_title'];
$pro_size = $row_products['product_size'];
$pro_price = $row_products['product_price'];
$pro_image = $row_products['product_image'];
?>
<div class="col-lg-2 col-12">
<input type="hidden" name="price" value="">
<input type="hidden" name="product_title" value="<? echo $pro_title; ?>">
<input type="hidden" name="product_image" value="<? echo $pro_image; ?>">
<div class="card mb-3">
<div class="tw-aspect-w-4 tw-aspect-h-4">
<img src="../includes/product_images/<? echo $pro_image ?>" class="card-img-top" alt="...">
</div>
<div class="card-body">
<h5 class="card-title"><? echo $pro_title ?></h5>
<p class="card-text mb-3">Price <? echo $pro_price ?> EUR</p>
<a href="#" class="btn btn-primary">Go somewhere</a>
</div>
<div class="card-footer">
<div class="form-check mb-2">
<input type="checkbox" class="form-check-input" name="pid[]" id="<?echo $pro_id?>" value="<?echo $pro_id?>">
<label class="form-check-label" for="<?echo $pro_id?>">
Select
</label>
</div>
</div>
</div>
</div>
<? } ?>
</form>
and in my add_selection_script.php
<?php
include '../includes/conn.php';
if (isset($_POST['insert_selection'])) {
$u_id = $_POST['u_id'];
$tradexx_price = $_POST['tradexx_price'];
$product_title = $_POST['product_title'];
$product_image = $_POST['product_image'];
$pid = implode(',', $_POST['pid']);
$insert_selection = "INSERT INTO selection (c_id,p_id,tradexx_price,pvp,product_name,img) VALUES ('$u_id','$checkBox','$tradexx_price','$pvp','$product_title','$product_image')";
echo $insert_selection;
die();
$insert_sel = mysqli_query($con, $insert_selection);
if ($insert_sel) {
echo "<script>alert('Selection has been added!')</script>";
echo "<script>window.open('add_selection.php', '_self')</script>";
}
}
?>
I do can get the pid
comma separate form but how can I get the others?
i am not good at PHP and trying to figure it out.
my goal is to insert all checked products as separate entries in database.
Thanks