Codeigniter: Unknown column 'stud.id' in 'where clause'

Viewed 532

I encounter the error "Unknown column 'stud.id' in 'where clause'. I don't have any stud.id in my files and also in my database. What do you think the problem with this? I'm using Codeigniter 4.

Below is my code on my Controller:

    $model = new Mod_Stud();
    $id = $this->request->getVar('stud_id');

    $data = [
                'stud_id' => $this->request->getVar('stud_id'),
                'lname' => $this->request->getVar('lname'),
                'fname' => $this->request->getVar('fname'),
                'mname' => $this->request->getVar('mname'),
    ];

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

Code for my Model:

    protected $table = 'stud';
    protected $allowedFields = ['stud_id', 'sy', 'dpt', 'grd', 'lname', 'fname', 'mname', 'gender', 'bday', 'birthplace', 'religion', 'nationality', 'country', 'address', 'f_name', 'f_occ', 'm_name', 'm_occ', 'g_name', 'g_rel', 'created_at', 'updated_at'];

I already fix it!

Below is the codes that I use.

$model->set($data);
$model->where('stud_id', $id);
$model->update();
3 Answers

update after comments:

you have in your update() function a wrong parameter sequence, it should be

$save = $model->update($data, ['id' => $id]);

See Updating Data

This answer assumes you loaded your query builder correctly

unknown-column-stud-id-in-where-clause :- This error is arises due to database ...because id column which is used is not available on database or it is incorrect ..let us suppose I have 3 columns in my database table "ID" , "Name" , "NO"

So if need to select "ID" i have to use keyword "ID" , if i use "id" instead of "ID" then this error (unknown-column-stud-id-in-where-clause) happens. Please check your database column and copy paste them in your where clause which you need to select .

Probably this will help,

protected $primaryKey = 'stud_id';

remove stud_id from your protected $allowedFields

Related