首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从PHP代码点火器中的字段中减去一个值?

如何从PHP代码点火器中的字段中减去一个值?
EN

Stack Overflow用户
提问于 2015-09-02 11:46:56
回答 4查看 6.4K关注 0票数 1

我有一个餐桌分销商和用户。现在,每个用户都被他的分销商映射为一个名为KEY的字段,该字段唯一地定义了分销商!现在我有了一个编辑用户的表单。在这里,如果经销商将用户余额设置为“是”,那么我希望从分销商的余额字段中减去5欧元。下面是我的代码:

负责检查分销商是否已将余额设置为“是”的控制器:

代码语言:javascript
复制
 if($this->input->post('balance') === "Yes")
         {
            $this->reseller_m->deduct();
         }

从经销商平衡中减去5的模式函数:

代码语言:javascript
复制
public function deduct($balance)
{
    $this->db->set('balance', 'balance-5');
    $this->db->where('id' , $id);
    $this->db->update('reseller',$data);
}

编辑代码:

代码语言:javascript
复制
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,只有当他编辑自己的用户时,不应该影响其他人。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2015-09-02 12:01:56

在您的模型中尝试这样做,其中$id是分销商的id。

代码语言:javascript
复制
public function deduct($id)
{
    $this->db->set('balance', 'balance-5', false);
    $this->db->where('id' , $id);
    $this->db->update('reseller');
}

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

代码语言:javascript
复制
 if($this->input->post('balance') === "Yes")
 {
    $this->reseller_m->deduct($id); //you must have the $id (reseller's id) value set
 }
票数 2
EN

Stack Overflow用户

发布于 2015-09-02 11:50:16

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

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

代码语言:javascript
复制
$this->db->set('balance', 'balance-5',FALSE);

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

代码语言:javascript
复制
$this->db->update('reseller');

所以最终你的功能是

模型

代码语言:javascript
复制
 public function deduct($id)
    {
        $this->db->set('balance', 'balance-5',FALSE);
        $this->db->where('id' , $id);
        $this->db->update('reseller');
    }

控制器

代码语言:javascript
复制
if($this->input->post('balance') === "Yes")
         {

            $this->reseller_m->deduct($id);/// pass youe id here

            //echo "goimng";
         }
票数 2
EN

Stack Overflow用户

发布于 2015-09-02 11:49:53

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32352313

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档