【问题标题】:Using a loop to perform multiple SQL inserts using Codeigniter/Activerecord使用循环使用 Codeigniter/Activerecord 执行多个 SQL 插入
【发布时间】:2013-08-02 10:27:47
【问题描述】:

我有一个如下所示的数组:

Array
(
[currency] => GBP
[shipping] => 3.5
[tax] => 0
[taxRate] => 0
[itemCount] => 3

[item_name_1] => Proletarian Sparrow
[item_quantity_1] => 2
[item_price_1] => 75

[item_name_2] => Guillemot Colony
[item_quantity_2] => 5
[item_price_2] => 20

[item_name_3] => Dandelion Clock
[item_quantity_3] => 2
[item_price_3] => 50
)

我正在尝试使用循环来提取单个项目的详细信息,并在数据库中为每个项目插入一行。我正在使用 Codeigniter。

我的模型如下所示:

public function set_order($cust_id, $order_data)
{

    // Breaks up order information and creates a new row in the pn_orders tables for each item

    // Get the last row in the orders table so we can work out the next order_id
    $this->db->order_by('order_id', 'desc');
    $this->db->limit(1);
    $query = $this->db->get('pn_orders'); 

    $row = $query->row_array(); 

    // If the resulting array is empty, there are no orders so we can set the order_id to 1. If there is already an order_id, just add 1 to it.
    if (empty($row)) {
        $order_id = 1;
    } else {
        $order_id = $row['order_id'] + 1;
    }

    //echo "<pre>";
    //print_r($order_data);
    //echo "</pre>";

    // The delivery message input has a placeholder. if the user's input is different to this, assign it to $delivery_message.
    if ($this->input->post('delivery_message') == "e.g. if out, leave in porch") {
      $delivery_message = NULL;
    } else {
      $delivery_message = $this->input->post('delivery_message');
    }

    // Set today's date for insertion into the DB
    $date = date('Y-m-d');

    // The order information is clumped together in a single array. We have to split out single items by looping through the array before inserting them into the DB.
    for ($i = 1; $i <= $order_data['itemCount']; $i++) {

        $item = array(
            'order_id' => $order_id,
            'cust_id' => $cust_id,
            'date_ordered' => $date,
            'item_name' => $order_data["item_name_{$i}"],
            'item_quantity' => $order_data["item_quantity_{$i}"],
            'item_price' => $order_data["item_price_{$i}"],
            'payment_method' => $_POST['payment_method'],
            'delivery_message' => $delivery_message
        );

        $this->db->insert('pn_orders', $item);

     }

}

一切似乎都已经到位,但是,只插入了 1 行,我不知道为什么。看起来很简单。

这和 Activerecord 模式有关吗?

谢谢。

【问题讨论】:

    标签: php codeigniter loops activerecord


    【解决方案1】:

    print out数组,看看array structure is correct or not。如果没问题,那么只需像这样使用insert_batch

    for ($i = 1; $i <= $order_data['itemCount']; $i++) {
        $items[] = array(
            'order_id' => $order_id,
            'cust_id' => $cust_id,
            'date_ordered' => $date,
            'item_name' => $order_data["item_name_{$i}"],
            'item_quantity' => $order_data["item_quantity_{$i}"],
            'item_price' => $order_data["item_price_{$i}"],
            'payment_method' => $_POST['payment_method'],
            'delivery_message' => $delivery_message
        );
    }
    //echo "<pre>";print_r($item);echo "</pre>";die;   uncomment to see the array structure
    $this->db->insert_batch('pn_orders', $items);
    

    【讨论】:

    • 谢谢,我已经测试了你的建议。您制作的数组打印得很好(print_r(items))。但是,现在没有插入任何行。如您所知,数据库库已加载。
    • 回显这一行,写在插入语句echo $this-&gt;db-&gt;last_query();die;之后。获取该查询并在您的 phpmyadmin 中运行。它是否给您任何数据库错误或什么?
    • 乐于助人:D @ChrisJ
    • 知道了! order_id 上有一个重复的条目,因为它是在数据库的主键中设置的。没有你的帮助就不会解决这个问题。您还以一种非常优雅的方式帮助我学习了 insert_batch 的实际用法。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    • 2019-02-06
    • 2013-08-11
    • 1970-01-01
    • 2020-12-27
    • 1970-01-01
    相关资源
    最近更新 更多