【问题标题】:At the time of update my where condition is not working在更新时,我的 where 条件不起作用
【发布时间】:2020-01-16 22:52:20
【问题描述】:

在我的第一个table(ts_users) 更新打开user_opening_balance 的同时,我想在我的第二个table(ts_voucher) 列中更新user_opening_balancevoucher_amount。但是在我的第二张表中 table(ts_voucher) voucher_amount 更新了所有列的数量。

我的代码:

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;
      }

    }

我给出了 where 条件

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

为了更新一条记录,它会更新所有 voucher_amount 列记录

【问题讨论】:

    标签: php mysql sql codeigniter


    【解决方案1】:

    您需要先运行.where,然后再运行.update。见文档here

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

    【讨论】:

      【解决方案2】:

      在“where”条件有机会起作用之前,您的更新操作就开始起作用了。这就是它更新所有记录的原因。只需先放置 where 条件,然后再更新。

      【讨论】:

        【解决方案3】:

        您的更新查询错误。 where 子句 first 和 where first than 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;
          }
        

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-08-30
        • 1970-01-01
        • 2022-01-04
        • 2023-03-27
        • 2013-12-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多