0

When I sending the data to the server in PHP with different input and same name I am not able to insert in my database through foreach function. I tried my best and all others functions available on stackoverflow but they all are not helpful for me.

Please help me how to make this fix and what will the real code to achieve my code functions. as developer console sending data is -

productID: 21202,33201,44202,44202,33204

Qty: 1,2,3,4,5

PHP

foreach($_POST['productID'] as $product) {
   foreach($_POST['Qty'] as $qty) {
      $stmt = $con->prepare("INSERT INTO `table` (product,qty) VALUES (:p,:q)");
      $stmt->execute(array(
       ':p' => $product,
       ':q' => $qty 
      ));
   }
}

echo $_POST['productID'];
response is = 21202,33204,332061
12
  • 2
    Please do a var_dump($_POST) and add the output to your question. We need to see the complete $_POST array to know what it contains. Commented Oct 27, 2020 at 11:48
  • 2
    That looks like a comma separated string, not an array. Try foreach(explode(',', $_POST['productID']) as $product) { foreach(explode(',', $_POST['Qty']) as $qty) { Commented Oct 27, 2020 at 11:49
  • 2
    You should really call prepare() before the loop, then call execute() with the new values inside the loop. Commented Oct 27, 2020 at 11:50
  • 1
    My guess is that you have the same issue with Qty, but since you haven't added what I asked for, I'm just guessing. Commented Oct 27, 2020 at 11:51
  • 1
    I asked for var_dump($_POST), not var_dump($_POST['productID']). I've written the exact code snippet three times now. Just copy/paste what I wrote instead of trying to make your own version. Commented Oct 27, 2020 at 11:54

1 Answer 1

2

It would appear your input data are comma separated strings of values, not arrays. To iterate over them, you need to convert them to an array using explode:

$stmt = $con->prepare("INSERT INTO `table` (product,qty) VALUES (:p,:q)");
foreach(explode(',', $_POST['productID']) as $product) {
   foreach(explode(',', $_POST['Qty']) as $qty) {
      $stmt->execute(array(
       ':p' => $product,
       ':q' => $qty 
      ));
   }
}

Note that you only need to prepare the statement once, so I've moved that outside the loop. You can also further optimise this code by binding parameters outside the loop:

$stmt = $con->prepare("INSERT INTO `table` (product,qty) VALUES (:p,:q)");
$stmt->bindParam(':p', $product);
$stmt->bindParam(':q', $qty);
foreach(explode(',', $_POST['productID']) as $product) {
   foreach(explode(',', $_POST['Qty']) as $qty) {
      $stmt->execute();
   }
}

Note that the above code will insert all combinations of productID and Qty values into the table (as does your original code), but you probably only want the matching values. In that case, use this code:

$stmt = $con->prepare("INSERT INTO `table` (product,qty) VALUES (:p,:q)");
$stmt->bindParam(':p', $product);
$stmt->bindParam(':q', $qty);
$quantities = explode(',', $_POST['Qty']);
foreach(explode(',', $_POST['productID']) as $key => $product) {
    $qty = $quantities[$key];
    $stmt->execute();
}
Sign up to request clarification or add additional context in comments.

10 Comments

@JohnCart absolutely, assuming $con is a PDO connection
@JohnCart I've just tested this exact code on my server with $_POST = array('productID' => '21202,51101,44202', 'Qty' => '4,5,6'); and it inserts 9 rows into the table
i believe it should insert only three
@JohnCart please see my latest edit, that will only insert matching values of ProductID and Qty, so for $_POST = array('productID' => '21202,51101,44202', 'Qty' => '4,5,6'); it inserts three rows of 21202 4 51101 5 44202 6
@Nick Please help when I var_dump I get the response ` ["productID"]=> string(17) "21202,21202,21202" ["Qty"]=> string(5) "1,2,3"`
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.