Jump to content
  • разработка интернет магазинов на opencart
  • доработка интернет магазинов на opencart

Множественный INSERT INTO в подготовленном запросе PDO


Recommended Posts

Добрый гдень. Подскажите, как правильно выполнить множественный запрос на запись данных использую PDO и подготовленный запрос.

Вот код

Скрытый текст
$resultQuery = $this->db->prepare($query);

if ($queryParam) {
    foreach ($queryParam as $key => $valueParam) {
       foreach ($valueParam as $param => &$dataParam) {
          $resultQuery->bindParam(":$param", $dataParam['data'], $dataParam['type']);
       }
    }
}

$resultQuery->execute();

 

Данные формирует данный метод:

Скрытый текст
private static function insertOrderProduct($productData, $orderId)
    {
        if (!empty($productData) || !empty($orderId)) {
            $db = Registry::get('db');
            $query = 'INSERT INTO orders_product ( '
                . 'order_id, product_id, quantity'
                . ') VALUES ( '
                . ':order_id, '
                . ':product_id, '
                . ':quantity '
                . ')';
            foreach ($productData as $key => $product) {
                $queryParam[] = array(
                    'order_id' => array(
                        'data' => $orderId,
                        'type' => PDO::PARAM_STR
                    ),
                    'product_id' => array(
                        'data' => $product['product_id'],
                        'type' => PDO::PARAM_STR
                    ),
                    'quantity' => array(
                        'data' => $product['quantity'],
                        'type' => PDO::PARAM_STR
                    )
                );
            }
            $db->query($query, $queryParam);
        }
    }

 

Вот массив, который он формирует:

Скрытый текст
Array
(
    [0] => Array
        (
            [order_id] => Array
                (
                    [data] => 116
                    [type] => 2
                )

            [product_id] => Array
                (
                    [data] => 66
                    [type] => 2
                )

            [quantity] => Array
                (
                    [data] => 1
                    [type] => 2
                )

        )

    [1] => Array
        (
            [order_id] => Array
                (
                    [data] => 116
                    [type] => 2
                )

            [product_id] => Array
                (
                    [data] => 65
                    [type] => 2
                )

            [quantity] => Array
                (
                    [data] => 1
                    [type] => 2
                )

        )

    [2] => Array
        (
            [order_id] => Array
                (
                    [data] => 116
                    [type] => 2
                )

            [product_id] => Array
                (
                    [data] => 64
                    [type] => 2
                )

            [quantity] => Array
                (
                    [data] => 1
                    [type] => 2
                )

        )

)

 

Структура иблицы:

Скрытый текст

Проблема заключается в том, что в иблицу зноиет только первая запись.

 

Link to comment
Share on other sites

Что в параметрах гделает data и type?

Почему массив вместо строки в параметры пускаете?

 

Ещё можно заменить драйвер и попропотому чтовать запрос написать, как для mysql

https://github.com/opencart/opencart/blob/master/upload/system/library/db/pdo.php

 

Тут в примере нет никаких data и type

https://phpdelusions.net/pdo_examples/insert

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

On our site, cookies are used and personal data is processed to improve the user interface. To find out what and what personal data we are processing, please go to the link. If you click "I agree," it means that you understand and accept all the conditions specified in this Privacy Notice.