如何更新表中的多行?

时间:2018-12-28 08:15:44

标签: php codeigniter sql-update

我必须使用名为daytot的数组中的值更新表$result3

这怎么办?

$this->db->select('tdate');
$this->db->where('tdate >=', $newDate);
$this->db->where('tdate <=', $date);
$result3=$this->db->get('daytot')->result_array();
$amount=$this->input->post('credit1');
for($i=0;$i<count($result3);$i++) {
    $opcash=$result3[$i]['opcash']+$amount;
    $data1 = array(
        'tdate'=>$newDate,
        'total_credit'=>$total_credit['amount'],
        'total_debit'=>$total_debit['amount'],
        'opcash'=>$opcash,
    ;
    $this->db->where('tdate', $result3[$i]);
    $this->db->update('daytot', $data1);
}

4 个答案:

答案 0 :(得分:0)

尝试使用foreach循环更新记录

$result3数组结构是

{ ["tdate"]=> string(10) "24/12/2018" ["opcash"]=> string(3) "500" } { ["tdate"]=> string(10) "25/12/2018" ["opcash"]=> string(3) "1000" }

所以尝试这样。并确保您拥有所有变量值

$this->db->select('tdate');
$this->db->where('tdate >=', $newDate);
$this->db->where('tdate <=', $date);
$result3=$this->db->get('daytot')->result_array();
$amount=$this->input->post('credit1');

现在检查$result3是否有记录

if(isset($result3) && count($result3) > 0){
foreach($result3 as $result) {
    $opcash=$result['opcash']+$amount;
    $data1 = array(
    'tdate'=>$newDate,
    'total_credit'=>$total_credit['amount'],
    'total_debit'=>$total_debit['amount'],
    'opcash'=>$opcash,
   ;
    $this->db->where('tdate', $result['tdate']);
   $this->db->update('daytot', $data1);
}
}

答案 1 :(得分:0)

请尝试以下方法。 id是表daytot中的唯一值,您可以使用任何唯一字段。

$this->db->select('id, tdate, opcash'); //id => primary key
$this->db->where('tdate >=', $newDate);
$this->db->where('tdate <=', $date);
$result3 = $this->db->get('daytot')->result_array();
$amount  = $this->input->post('credit1');

使用

$data1 = array();
for ($i = 0; $i < count($result3); $i++) {
    $opcash = $result3[$i]['opcash'] + $amount;
    $data1[]  = array(
        'id'        => $result3[$i]['id'],
        'tdate'        => $newDate,
        'total_credit' => $total_credit['amount'],
        'total_debit'  => $total_debit['amount'],
        'opcash'       => $opcash
    );    
}
$this->db->update_batch('daytot', $data1, 'id');

或使用Foreach

foreach($result3 as &$data){
    $opcash = $data['opcash'] + $amount;
    $data['tdate'] = $newDate;
    $data['total_credit'] = $total_credit['amount'];
    $data['total_debit'] = $total_debit['amount'];
    $data['opcash'] = $opcash;    
}
$this->db->update_batch('daytot', $result3, 'id');

答案 2 :(得分:0)

首先,您还需要选择opcash列:

$this->db->select('tdate,opcash');

然后在每次0迭代中应用索引foreach

if(isset($result3) && count($result3) > 0){
    foreach($result3 as $r) {
        $opcash=$r[0]['opcash']+$amount;
        $data1 = array(
            'tdate'=>$newDate,
            'total_credit'=>$total_credit['amount'],
            'total_debit'=>$total_debit['amount'],
            'opcash'=>$opcash,
        );
        $this->db->where('tdate', $r[0]['tdate']);
        $this->db->update('daytot', $data1);
    }
}

答案 3 :(得分:-1)

AvailableRoom::where('roomTypeID', '=',  $request->session()->get('room_type'))
              ->update(['isAvailable'=> 0])

update batch (bulk) in laravel