0
I’m trying to insert values from a array inside the database using PHP, but I’m having difficulty in the applied method. It’s the first time I use arrays for that reason.
My current code is:
<input type='text' id="productName[]" name="productName[]">
<input type='text' id="quantity[]" name="quantity[]">
include_once("connection.php");
$name = $_POST['productName'];
$quantity = $_POST['quantity'];
$name = '"' . implode('","', $name) . '"';
$quantity = '"' . implode('","', $quantity) . '"';
$sql = "SELECT * FROM product WHERE name IN ($name)";
$result = mysqli_query($conn_app, $sql);
if (mysqli_affected_rows($conn_app) > 0){
while($row = mysqli_fetch_assoc($result)){
$itemId = $row['product_id'];
};
$insert = "INSERT INTO item_box (quantity,product_id) VALUES ('$quantity','$itemId')";
$result = mysqli_query($conn_app, $insert);
I need each product and its quantity to be entered in the database. For example:
INSERT INTO item_box (quantity, product_id)VALUES (13,2),(23,3),(40,3);
Currently the code points error in the syntax of SQL, for it is being returned thus:
INSERT INTO item_box (quantity,product_id) VALUES ("20","30","40","50","2")
Buddy, thanks, it worked right but it’s inserting duplicate, knows how to solve?
– JS Santana
In case the same data is being entered in the same amount of arrays
– JS Santana
I made changes to the answer, this should avoid the problem you reported
– Bins