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 myadd_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 thepid
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
Right now you have this inside a loop:
<input type="hidden" name="price" value="">
This gives you multiple uses of the same nameprice
, which isn't going to work. I think the best way to handle this is to use your product id as the value for the checkbox:
<input type="checkbox" name="pid[]" value="<?php echo $pro_id; ?>">
Then, in the page that processes the form, you can just loop over the array:
foreach ($_POST['pid'] as $pid) {
// $pid now contains the product id to insert
}
I also note that all the values you're inserting are coming fromhidden
form elements. Don't do this. Even though the values are hidden, that won't stop the user from being able to provide new values. Instead, just take the product id, and then lookup the associated values at the time of the insert. Something like this:
foreach ($_POST['pid'] as $pid) {
// $product = SELECT * FROM products WHERE product_id = $pid
// now you have $product['product_image'] to insert into the selection table
}
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
DBMS is a database management system. It is designed to change, search, add and delete information in the database. There are many DBMSs designed for similar purposes with different features. One of the most popular is MySQL.
It is a software tool designed to work with relational SQL databases. It is easy to learn even for site owners who are not professional programmers or administrators. MySQL DBMS also allows you to export and import data, which is convenient when moving large amounts of information.
https://www.mysql.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.