简体   繁体   English

如何使用codeigniter在mysql中保存多行

[英]how to save multiple rows in mysql using codeigniter

I have a form for a hotel site, where I want to update its services, and the client wants to update multiple services at a time. 我有一个旅馆网站的表单,我想在其中更新其服务,而客户希望一次更新多个服务。 However I'm lost as how to save it in the database with the model. 但是,我不知道如何使用模型将其保存在数据库中。

I already built my controller, it looks something like this: 我已经建立了控制器,它看起来像这样:

$items = array(
        array(
                'id'           => $this->input->post('id1', true),
                'hotel_id'     => $hotel_id,
                'des_servicio' => $this->input->post('des_servicio1', true),
                'est_activo'   => $this->input->post('est_activo1', true)
        ),
        array(
                'id'           => $this->input->post('id2', true),
                'hotel_id'     => $hotel_id,
                'des_servicio' => $this->input->post('des_servicio2', true),
                'est_activo'   => $this->input->post('est_activo2', true)
        ),
        array(
                'id'           => $this->input->post('id3', true),
                'hotel_id'     => $hotel_id,
                'des_servicio' => $this->input->post('des_servicio3', true),
                'est_activo'   => $this->input->post('est_activo3', true)
        )
);

$this->hotel_model->save_multiple($items);

[UPDATE] [更新]

this is how my new model looks like: 这是我的新模型的样子:

function save_multiple($items = array())
{
    $this->db->insert_batch($this->__tabla, $items);
    return $this->db->affected_rows();
}

My issue is that now it creates 10 rows (my original form has 10 fields) even if I only populate 3 fields. 我的问题是,即使我仅填充3个字段,它现在也会创建10行(我的原始表单具有10个字段)。 So in my database 3 services get stored, and also 7 blank rows. 因此,在我的数据库中,存储了3个服务以及7个空白行。 How can change this? 如何改变呢?

foreach $items //I get an error here
    as $item 

should be 应该

foreach ( $items as $item ) 

Remember that input->post() returns false if the value is not set. 请记住,如果未设置值,则input-> post()返回false。 So check to see if the value is set be for putting it in the array. 因此,请检查该值是否设置为将其放入数组中。 Then when the model receives it. 然后当模型收到它时。 It saves them all. 它保存了所有。 Or the other option is to create a new array in the model from the array that is passed in and then pass the new array to the insert_batch() function. 或另一个选择是从传入的数组在模型中创建一个新数组,然后将新数组传递给insert_batch()函数。

$items = array();

$id = $this->input->post('id1', true);
if( $id != false ) {
    $items[] = array(
        'id'           => $id, true),
        'hotel_id'     => $hotel_id,
        'des_servicio' => $this->input->post('des_servicio1', true),
        'est_activo'   => $this->input->post('est_activo1', true)
    );
}

$id = $this->input->post('id2', true);
if( $id != false ) {
    $items[] = array(
        'id'           => $id, true),
        'hotel_id'     => $hotel_id,
        'des_servicio' => $this->input->post('des_servicio2', true),
        'est_activo'   => $this->input->post('est_activo2', true)
    );
}
....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM