简体   繁体   English

在更新时,我的条件不起作用

[英]At the time of update my where condition is not working

In my first table(ts_users) update opening user_opening_balance at the same time, I want to update that user_opening_balance in my second table(ts_voucher) column is voucher_amount . 在我的第一个table(ts_users)更新开放user_opening_balance在同一时间,我想更新user_opening_balance在我的第二个table(ts_voucher)voucher_amount But in my second table table(ts_voucher) voucher_amount updates all columns amount. 但是在我的第二张表table(ts_voucher) voucher_amount更新了所有列的金额。

My code: 我的代码:

public function updateConsignor($myData){
      extract($myData);

      $this->db->set('user_full_name' , $user_full_name);
      $this->db->set('user_opening_balance' , $user_opening_balance);

    $this->db->where('user_id', $user_id);

      if($this->db->update('ts_users')){

         $userId      = $myData['user_id'];
          $this->db->trans_begin();
                                $openingBalTrxn = array(
                                'voucher_amount'               => $myData['user_opening_balance'],
                                );

          $this->db->update('ts_voucher', $openingBalTrxn);
          $this->db->where('voucher_person_account_id',$userId);

            if ($this->db->trans_status() === false){
                      $this->db->trans_rollback();
                      return false;
             }else{
                      $this->db->trans_commit();
                      return true;
                  }

           return $query_result;

           return true;
      }else{

        return false;
      }

    }

I am giving where condition 我给条件

$this->db->update('ts_voucher', $openingBalTrxn);
$this->db->where('voucher_person_account_id',$userId);

for update one record it updates all voucher_amount column record 对于更新一条记录,它将更新所有voucher_amount列记录

在此处输入图片说明

you need to run first your .where before your .update . 你需要先运行.where你之前.update See documentation here 在这里查看文档

$this->db->where('voucher_person_account_id',$userId);
$this->db->update('ts_voucher', $openingBalTrxn);

Your update query false. 您的更新查询为false。 where clause first and other condition where first than update function call just copy this function and fix your problem. where子句first和其他条件(其中update函数首先调用)仅复制此函数并解决您的问题。

public function updateConsignor($myData){
  extract($myData);

  $this->db->set('user_full_name' , $user_full_name);
  $this->db->set('user_opening_balance' , $user_opening_balance);

$this->db->where('user_id', $user_id);

  if($this->db->update('ts_users')){

     $userId      = $myData['user_id'];
      $this->db->trans_begin();
                            $openingBalTrxn = array(
                            'voucher_amount'               => $myData['user_opening_balance'],
                            );
      $this->db->where('voucher_person_account_id',$userId);
      $this->db->update('ts_voucher', $openingBalTrxn);

         if ($this->db->trans_status() === false){
                  $this->db->trans_rollback();
                  return false;
         }else{
                  $this->db->trans_commit();
                  return true;
              }

       return $query_result;

       return true;
  }else{

    return false;
  }

Your update operation is working before the 'where' condition has a chance to work. 您的更新操作在“ where”条件有机会起作用之前就已经起作用。 That's why it's updating all records. 这就是为什么它更新所有记录。 Simply put the where condition first, then update. 只需简单地将where条件放在首位,然后更新即可。

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

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