Pdo multiple row insert issue

speedy

enter image description here

I'm having some problem with inserting multiple rows in mysql with pdo prepare statements.

The insert works but only once.

I have tried to put the query before the foreach, but same problem.

  $product_id = $_POST['id'];
  if (isset($_POST['product_attribute'])){
        $att_id = array();

    foreach ($_POST['product_attribute'] as $product_attribute) {

      $att_id[] = $product_attribute['attribute_id'];

      if ($product_attribute['attribute_id']) {
          $stmt = $database->prepare("DELETE FROM product_attribute WHERE product_id =:product_id AND attribute_id=:attribute_id");
          $stmt->bindParam(':product_id', $product_id, PDO::PARAM_INT);
          $stmt->bindParam(':attribute_id',  $product_attribute['attribute_id'], PDO::PARAM_INT);
          $stmt->execute();


        foreach ($product_attribute['product_attribute_description'] as $product_attribute_description) {
          $stmt = $database->prepare("INSERT INTO product_attribute SET product_id=:product_id, product_code=:product_code, attribute_id=:attribute_id, `text`=:atext");
          $stmt->bindParam(':product_id', $product_id,  PDO::PARAM_INT);
          $stmt->bindParam(':product_code', $_POST['codprodus'], PDO::PARAM_INT);
          $stmt->bindParam(':attribute_id', $product_attribute['attribute_id'], PDO::PARAM_INT);
          $stmt->bindParam(':atext',  $product_attribute_description['text'], PDO::PARAM_STR);
          $stmt->execute();

        }
      }
    }

    $vals = implode( ",", $att_id );
    $stmt = $database->prepare("DELETE FROM product_attribute WHERE product_id =:product_id AND attribute_id NOT IN(:vals)");
    $stmt->bindParam(':product_id', $product_id, PDO::PARAM_INT);
    $stmt->bindParam(':vals', $vals, PDO::PARAM_STR);
    $stmt->execute();

  }else {
    $stmt = $database->prepare("DELETE FROM product_attribute WHERE product_id =:product_id");
    $stmt->bindParam(':product_id',  $product_id, PDO::PARAM_INT);
    $stmt->execute();
  }

Print_r($_POST['product_attribute'] :

Array([103] => Array(
            [attribute_id] => 103
            [product_attribute_description] => Array
                (
                    [2] => Array
                        (
                            [text] => 56
                        )

                )

        )

    [102] => Array
        (
            [attribute_id] => 102
            [product_attribute_description] => Array
                (
                    [2] => Array
                        (
                            [text] => da
                        )

                )

        )

    [104] => Array
        (
            [attribute_id] => 104
            [product_attribute_description] => Array
                (
                    [2] => Array
                        (
                            [text] => da
                        )

                )

        )

)

And here is my product_attribute table schema.

product_id      int(11)     NO  PRI         
attribute_id    int(11)     NO  PRI         
product_code    int(11)     NO          
text            text        NO
Ryan Vincent

This code is deleting the newly inserted rows. Comment it out and all is fine.

/*
$vals = implode( ",", $att_id );
$stmt = $database->prepare("DELETE FROM product_attribute WHERE product_id =:product_id AND attribute_id NOT IN(:vals)");
$stmt->bindValue(':product_id', $product_id, PDO::PARAM_INT);
$stmt->bindValue(':vals', $vals, PDO::PARAM_STR);
$stmt->execute();
*/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

insert pdo query issue, multiple inserts in wrong table

From Dev

PDO multiple select issue

From Dev

PDO insert not inserting with multiple values

From Dev

PHP PDO Multiple image INSERT

From Dev

Insert post with multiple tags in PDO

From Dev

Yii multiple row insert

From Dev

my issue is that i insert multiple image in one row with comma separated in codeginter

From Dev

multiple PDO insert with single statement not working

From Dev

From Textarea to multiple value insert using PDO

From Dev

PDO Insert multiple checkbox values in Mysql

From Dev

Insert multiple rows using PHP PDO

From Dev

PHP, PDO, MySQL - Multiple INSERT vulnerable to injection?

From Dev

Join 1 Row to Multiple Rows in PDO

From Dev

Join 1 Row to Multiple Rows in PDO

From Dev

Multiple Row JQuery Dialog Issue

From Dev

insert multiple ID in mysql row

From Dev

get first insert id for multiple insert using pdo in mysql

From Dev

php PDO Insert issue (Ajax drop-down extraction)

From Dev

how to Insert multiple arrays with multiple rows into MySQL using PHP PDO

From Dev

insert into using same row(same primary key) mysql php pdo

From Dev

PDO - Insert new row when duplicate detected based on 3 values

From Dev

INSERT row from another table using php PDO

From Dev

insert into using same row(same primary key) mysql php pdo

From Dev

PDO SQL issue displaying multiple rows when using COUNT()

From Dev

What is the best way to insert multiple rows in PHP PDO MYSQL?

From Dev

How to insert multiple records at once with mysqli similar to pdo

From Dev

PDO and binding multiple value sets during insert - recently

From Dev

Insert multiple values via PHP PDO into MySQL database

From Dev

php pdo select multiple rows and insert to other table with LIMIT

Related Related

  1. 1

    insert pdo query issue, multiple inserts in wrong table

  2. 2

    PDO multiple select issue

  3. 3

    PDO insert not inserting with multiple values

  4. 4

    PHP PDO Multiple image INSERT

  5. 5

    Insert post with multiple tags in PDO

  6. 6

    Yii multiple row insert

  7. 7

    my issue is that i insert multiple image in one row with comma separated in codeginter

  8. 8

    multiple PDO insert with single statement not working

  9. 9

    From Textarea to multiple value insert using PDO

  10. 10

    PDO Insert multiple checkbox values in Mysql

  11. 11

    Insert multiple rows using PHP PDO

  12. 12

    PHP, PDO, MySQL - Multiple INSERT vulnerable to injection?

  13. 13

    Join 1 Row to Multiple Rows in PDO

  14. 14

    Join 1 Row to Multiple Rows in PDO

  15. 15

    Multiple Row JQuery Dialog Issue

  16. 16

    insert multiple ID in mysql row

  17. 17

    get first insert id for multiple insert using pdo in mysql

  18. 18

    php PDO Insert issue (Ajax drop-down extraction)

  19. 19

    how to Insert multiple arrays with multiple rows into MySQL using PHP PDO

  20. 20

    insert into using same row(same primary key) mysql php pdo

  21. 21

    PDO - Insert new row when duplicate detected based on 3 values

  22. 22

    INSERT row from another table using php PDO

  23. 23

    insert into using same row(same primary key) mysql php pdo

  24. 24

    PDO SQL issue displaying multiple rows when using COUNT()

  25. 25

    What is the best way to insert multiple rows in PHP PDO MYSQL?

  26. 26

    How to insert multiple records at once with mysqli similar to pdo

  27. 27

    PDO and binding multiple value sets during insert - recently

  28. 28

    Insert multiple values via PHP PDO into MySQL database

  29. 29

    php pdo select multiple rows and insert to other table with LIMIT

HotTag

Archive