10

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:

I have column names:

ID, Settings Name, Settings Value ( Settings Name is Unique )

I have the ff Data:

ID = 1, Settings Name = "Hello" , Settings Value = "True"
ID = 2, Settings Name = "World", Settings Value = "Good"

and more ...

I also have a form that gets the Settings Value but I am not sure how to update it on the DB. How to update the True for the Hello being the Settings Name and update the Good for the World.

I heard about insert_batch() but is there an update_batch()?

1

3 Answers 3

24

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for this!!! I have been staring at that same documentation from CI but I just have the light bulb when you answer this Q. hehe foolish me.. Thanks again!
I have a follow up Qs: How to retrieve from the DB the posted values? is it the normal set_value() ? Thanks!
@JeffSimonsDecena, what do you mean retrieve from DB with normal set_value()? The set_value() in CI's documentation is used to repopulate a form. Retrieving data from the DB can be found here Selecting Data
8

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.

Comments

1

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

1 Comment

That's a bad way of doing multiple row updates, as you are running 2 or more queries, where you can run only 1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.