我有一个餐桌分销商和用户。现在,每个用户都被他的分销商映射为一个名为KEY的字段,该字段唯一地定义了分销商!现在我有了一个编辑用户的表单。在这里,如果经销商将用户余额设置为“是”,那么我希望从分销商的余额字段中减去5欧元。下面是我的代码:
负责检查分销商是否已将余额设置为“是”的控制器:
if($this->input->post('balance') === "Yes")
{
$this->reseller_m->deduct();
}从经销商平衡中减去5的模式函数:
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,只有当他编辑自己的用户时,不应该影响其他人。
发布于 2015-09-02 12:01:56
在您的模型中尝试这样做,其中$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
}发布于 2015-09-02 11:50:16
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";
}发布于 2015-09-02 11:49:53
试试$this->db->set('balance', 'balance-5', FALSE);
https://stackoverflow.com/questions/32352313
复制相似问题