我的查询正在更新所有记录,而不是仅更新1,codeigniter4

时间:2020-07-22 09:46:20

标签: php database codeigniter

大家好,这是我的更新记录功能的摘录

public function updateRecord()
{
    helper(['form', 'url']);
    $model = new EmployeeModel();

    $id = $this->request->getVar('emp_id');
    echo $id;

    $data = [
        'emp_fname' => $this->request->getVar('emp_fname'),
        'emp_lname' => $this->request->getVar('emp_lname'),
        'emp_mname' => $this->request->getVar('emp_mname'),
        'emp_status' => $this->request->getVar('emp_status'),
        'emp_position' => $this->request->getVar('emp_position'),
    ];

    $save = $model->update($id, $data);
    
    return redirect()->to(base_url('display'));
}

2 个答案:

答案 0 :(得分:1)

在您的模型中,将 primaryKey 配置选项更改为您的自定义主键名称:

受保护的 $primaryKey = 'emp_id';

https://codeigniter4.github.io/userguide/models/model.html#models

答案 1 :(得分:0)

我解决了这个问题,看来在数据库上使用自定义ID不适用于CI4中的查询语句。

我在数据库本身中将emp_id更改为id,它解决了该问题。