Update multiple Rows in Codeigniter

前端 未结 3 1053
闹比i
闹比i 2020-12-16 20:41

I have been looking around but I have not found an answer yet. Kindly help if you know the answer.

How do you update multiple rows in CI?

In my MySQL:

<
相关标签:
3条回答
  • 2020-12-16 20:55

    Here is a simple php code for perform this operations.

    <?php
    function basic_update($uwi='') {
    $this->load->database();
    $data = array(
    'ID' => 1,
    'Settings Name' => 'Hello',
    'Settings Value' => 'True'  
    );
    $this->db->where('ID', '1');
    $this->db->update('<table name>', $data);
    $data1 = array(
        'ID' => 2,
        'Settings Name' => 'World',
        'Settings Value' => 'Good'
        );
        $this->db->where('ID', '2');
        $this->db->update('<table name>', $data1);
    }
    

    In $this->db->where('ID', '1'); ID is your table field and 1 is the value. In array first parameter will contain the table name, the second parameter is an associative array of values

    0 讨论(0)
  • 2020-12-16 20:56

    There is indeed an update_batch() method available in CodeIgniter already.

    You can use it your example like so:

    $data = array(
        array(
            'ID' => 1,
            'Settings Name' => 'Hello',
            'Settings Value' => 'True'
        ),
        array(
            'ID' => 2,
            'Settings Name' => 'World',
            'Settings Value' => 'Good'
        )
    );
    $this->db->update_batch('tableName', $data, 'id'); 
    

    So what you have is an array of arrays, the children basically hold the data for each row in the database. The first parameter for update_batch() is the name of the database table, the second is the $data variable and the third is the column you want to use in the WHEN clause.

    0 讨论(0)
  • 2020-12-16 21:01

    Are you using Active record?

    Yes there is an update batch: $this->db->update_batch();

    $data = array(
    array(
      'ID' => 1 ,
      'Settings Name' => 'Hello' ,
      'Settings Value' => 'True'
    ),
    array(
      'ID' => '2' ,
      'Settings Name' => 'World' ,
      'Settings Value' => 'Good'
    )
    );    
    
    $this->db->update_batch('mytable', $data, 'where_key'); 
    

    From the documentation:

    The first parameter will contain the table name, the second is an associative array of values, the third parameter is the where key.

    0 讨论(0)
提交回复
热议问题