【问题标题】:how to subtract a value from a field in PHP codeigniter?如何从 PHP codeigniter 中的字段中减去一个值?
【发布时间】:2015-09-02 11:46:56
【问题描述】:

我有一个表经销商和用户。现在每个用户都由他的经销商映射到一个名为 KEY 的字段,该字段唯一地定义了经销商!现在我有一个表单,我可以在其中编辑用户。在这里,如果经销商将用户余额设置为是,那么我想从经销商的余额字段中减去 5 欧元。以下是我的代码:

控制器检查经销商是否已将余额设置为是:

 if($this->input->post('balance') === "Yes")
         {
            $this->reseller_m->deduct();
         }

从经销商余额中减去 5 的 Mode 函数:

public function deduct($balance)
{
    $this->db->set('balance', 'balance-5');
    $this->db->where('id' , $id);
    $this->db->update('reseller',$data);
}

编辑代码:

public function edit ($id = NULL)
{

     $usertype=$this->session->userdata('usertype');
    if($usertype ==="reseller")
    {

    // Fetch a user or set a new one
    if ($id) {
        $this->data['user'] = $this->user_m->get($id);
        count($this->data['user']) || $this->data['errors'][] = 'User could not be found';
    }
    else {
        $this->data['user'] = $this->user_m->get_new();
    }

    // Set up the form
    $rules = $this->user_m->rules_admin;
    $id || $rules['password']['rules'] .= '|required';
    $this->form_validation->set_rules($rules);

    // Process the form
    if ($this->form_validation->run() == TRUE) {
        $data = $this->user_m->array_from_post(array('sip_id','sip_pass','name','email', 'password','phone','status','created','balance'));
        $data['password'] = $this->user_m->hash($data['password']);



        $this->user_m->save($data, $id);


         if($this->input->post('balance') === "Yes")
         {

            $this->reseller_m->deduct();

            //echo "goimng";
         }


        redirect('reseller/user');
    }

    // Load the view
    $this->data['subview'] = 'reseller/user/edit';
    $this->load->view('reseller/_layout_main', $this->data);


}
else{
    $this->load->view('permission');
}

}

只有当他编辑自己的用户并且不应该影响其他人时,才帮我从经销商那里减去 5。

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    在您的模型中试试这个,$id 是经销商的 ID。

    public function deduct($id)
    {
        $this->db->set('balance', 'balance-5', false);
        $this->db->where('id' , $id);
        $this->db->update('reseller');
    }
    

    此外,在您的控制器中,您必须知道经销商的 id,并在调用模型函数时使用它:

     if($this->input->post('balance') === "Yes")
     {
        $this->reseller_m->deduct($id); //you must have the $id (reseller's id) value set
     }
    

    【讨论】:

    • 我不应该传递密钥而不是 id 吗?
    • 嘿,这适合经销商模块。但我也有一个管理员用户,他也可以编辑经销商和用户。现在如果他编辑一个用户并将余额设置为是,余额将如何扣除?因为每个用户都通过一个名为 KEY 的字段与经销商进行映射,该字段是 uniuqe
    • 嘿,您的问题似乎已经有了答案。如果您还有其他问题,请转到此页面:stackoverflow.com/questions/ask
    • 是的,好吧 :) 但是你能告诉我你为什么使用 where id, $id 吗?
    • 我相信 Saty 也能回答这个问题,但你可以这样:$id 是表“经销商”中的用户索引。所以查询的意思是“为 id 为 $id 的经销商调整列余额”
    【解决方案2】:

    set() 还将接受一个可选的第三个参数 ($escape),如果设置为 FALSE,它将防止数据被转义。

    使用FALSE 作为你的集合中的第三个参数

    $this->db->set('balance', 'balance-5',FALSE);
    

    另外,您没有在函数中定义 $id and $data 并将更新更改为

    $this->db->update('reseller');
    

    所以最后你的功能是

    模型

     public function deduct($id)
        {
            $this->db->set('balance', 'balance-5',FALSE);
            $this->db->where('id' , $id);
            $this->db->update('reseller');
        }
    

    控制器

    if($this->input->post('balance') === "Yes")
             {
    
                $this->reseller_m->deduct($id);/// pass youe id here
    
                //echo "goimng";
             }
    

    【讨论】:

    • 嘿,感谢您的回答,但它并没有影响余额字段!知道我哪里错了
    • 为什么 OP 应该使用你的代码。你能详细说明一下吗
    • 因为你没有在你的函数中定义$id$data
    • ALso remove $data from update it would be $this->db->update('reseller');
    • 是的,我认为这可能是问题所在,但我从 deduct 函数中删除了所有行,我只保留了 $this->db->set('balance', 'balance-5',FALSE) ;
    【解决方案3】:

    试试 $this->db->set('balance', 'balance-5', FALSE);

    【讨论】:

    • 嘿,感谢您的回答,但它并没有影响余额字段!知道我哪里错了
    • 为什么 OP 应该使用你的代码。你能详细说明一下吗
    • 查看经销商登录到他的帐户。然后他可以查看他的余额并查看和编辑他的用户。现在,如果他编辑用户并将余额设置为是,我想每次他设置为是时扣除 5 欧元
    • $data 内有什么 $this->db->update('reseller',$data); <------ @Rajan
    • @NarendraSisodia 我已更新问题代码 $data 包含所有帖子字段
    【解决方案4】:

    为了使价值动态化,我将其用于产品库存更新

    这里,$purchase_qty 是已订购的项目。 $product_id 是订购的产品。

    $this->db->set("stock", "stock - $purchase_qty", FALSE)
            ->where('price_id', $price_id)
            ->where('product_id', $product_id)
            ->update('price_table');
    

    希望这会对你有所帮助。

    【讨论】:

      猜你喜欢
      • 2023-03-20
      • 1970-01-01
      • 2014-12-16
      • 1970-01-01
      • 2022-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多