2

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();
cjvdg
  • 497
  • 2
  • 15

3 Answers3

0

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

Vickel
  • 7,879
  • 6
  • 35
  • 56
  • can't be the same error, if you removed the array entry `'stud_id'`; and we don't know `´$id = $this->request->getVar('stud_id');´`, as you didn't show us yet... idf it is an array, you should `$save = $model->update($data, $id);` – Vickel May 28 '20 at 01:03
  • How can I show it? Sorry i'm just a beginner. I used var_dump to show the value of the stud_id and it shows the stud_id that im going to update then i also used it for the $data, it shows all the data for that specific id. But when i'm going to use $save = $model->update($data, ['id' => $id]); it shows error. – cjvdg May 28 '20 at 01:32
  • did you read the link in my answer? it says: *You’ll notice the use of the $builder->where() function, enabling you to set the WHERE clause. You can optionally pass this information directly into the update function as a string: $builder->update($data, "id = 4"); Or as an array: $builder->update($data, ['id' => $id]);* – Vickel May 28 '20 at 01:47
  • btw: you write *I don't have any stud.id in my files and also in my database*. So how do you want to update it (as in your comment above)? if it doesn't exist? – Vickel May 28 '20 at 01:49
  • The column name that I'm using is "stud_id", not "stud.id". – cjvdg May 28 '20 at 01:53
  • ok I see, I misread that. you need to debug `$this->request->getVar('stud_id');`and `$model = new Mod_Stud();` to see where that stud.id column comes from... could you please clarify that? – Vickel May 28 '20 at 01:58
  • The value of $this->request->getVar('stud_id'); is the stud_id that I'm going to update. Btw, I use the save function instead of the update and it works fine. It adds the data on my db. So why update don't work tho? – cjvdg May 28 '20 at 02:09
  • For my model, I use this: 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']; – cjvdg May 28 '20 at 02:10
0

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 .

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • i only have one id, no, name on my database. Here the column name: `stud_no`, `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 – cjvdg May 28 '20 at 00:44
  • here the problem is due to underscore sign try to replace it first .underscore replcaes with dot(.) that's why stud_id is relaces with stud.id so when you try to run query query builder try to find stud.id instead of stud_id. try to change coloumn name – Core of Geek May 28 '20 at 05:52
0

Probably this will help,

protected $primaryKey = 'stud_id';

remove stud_id from your protected $allowedFields

MnTanProject
  • 33
  • 1
  • 9