简体   繁体   中英

Codeigniter active record update with join

i have two tables:

table1: id, user_id, poll_id, options_id

table2: id, poll_id, votes

column votes is an integer and i want to change the value by joining the tables with some where clauses:

$this->db
->set('votes', 'votes - 1', FALSE)
->join('table1', 'poll_votes.options_id = table2.id')
->where('poll_id', $row)
->where('user_id', $id)
->update('table2');

I´m getting this error:

Error Number: 1054
Unknown column 'user_id' in 'where clause'
UPDATE `table2` SET votes = votes - 1 WHERE `poll_id` = '9' AND `user_id` = '1'

Try this one:

$this->db->set('votes', 'votes - 1', FALSE)
$this->db->where('table1.user_id',$id);
$this->db->where('table2.poll_id',$row);
$this->db->update('table1 join table2 ON table1.poll_id= table2.poll_id');

try this out. It worked for me!!!

$data = array(
    'a.ED_FName' => $_POST['firstname'],
    'a.ED_MName' => $_POST['middlename'],
    'a.ED_LName' => $_POST['lastname'],
    'a.ED_Location' => $_POST['location'],
    'a.ED_Company' => $_POST['company'],
    'b.EMD_Department' => $_POST['department']
);

$this->db->set($data);

$this->db->where('a.EmpID', $empID);

$this->db->where('b.EmpID', $empID);

$this->db->update('tbl_employeedetails as a, tbl_employeementdetails as b');

Best Regards, TUSHAR NIRAS

Update:

Note: Never Use Global Arrays Like $_POST, $_GET Containing User's Request Data Directly Without Security Checks. This Can Lead to Serious Issues Like SQL Injections.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM