Rahat Islam Khan
Rahat Islam Khan

Reputation: 135

Updating Multiple Rows in Codeigniter

I can insert multiple rows in the database, but the problem is when I am trying to update it gives me a Codeigniter error message. Here is my model, actually I have nothing significant in the controller to find an error in, is because I just load that model in controller.

$data  = array();
//$todayDate = date('Y-m-d');
for($i = 1; $i < count($_POST['code']); $i++) {
    //$code=$_POST['code'][$i];
    if($_POST['code'][$i] != '') {
        $data[] = array(
            $code='code' => $_POST['code'][$i],
            'price' => $_POST['sell']
            );
    }
}
$linksCount = count($data);

if($linksCount) {
    $this->db->where('code',$code);
    $this->db->insert_batch('sell_rate', $data);
}

return $linksCount;

Upvotes: 4

Views: 7194

Answers (1)

The Alpha
The Alpha

Reputation: 146191

In your Model following part should be

$data[] = array(
    $code='code' => $_POST['code'][$i],
    'price' => $_POST['sell']
);

replaced with

$data[] = array(
    'code' => $_POST['code'][$i],
    'price' => $_POST['sell']
);

and to update the values you should use update_batch instead of insert_batch

$this->db->update_batch('yourtableName', $data, 'code'); // 'code' is where key

Replace yourtableName with your original table name and code is being used for where key, so you don't need to use $this->db->where('code',$code).

Reference: CodeIgniter.

Upvotes: 3

Related Questions