【问题标题】:Codeigniter active record update with join带连接的 Codeigniter 活动记录更新
【发布时间】:2013-07-30 19:41:39
【问题描述】:

我有两张桌子:

table1:id、user_id、poll_id、options_id

table2: id, poll_id, votes

column votes 是一个整数,我想通过使用一些 where 子句加入表来更改值:

$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');

我收到此错误:

错误号:1054 'where 子句'中的未知列'user_id' UPDATE `table2` SET votes = votes - 1 WHERE `poll_id` = '9' AND `user_id` = '1'

【问题讨论】:

  • 再次检查 user_id 列名是否与 DB 表中的相同!
  • 是的,@ErmanBelegu 是一样的
  • 尝试回显您的最后一个查询并通过 phpmyadmin 将其放入数据库。 echo $this->db->last_query();也可以在这里发布

标签: php mysql codeigniter activerecord sql-update


【解决方案1】:

试试这个:

$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');

【讨论】:

  • 只需用$this->db->update('table1 JOIN table2 ON table1.poll_id= table2.poll_id');更新这部分$this->db->update('table1 join ON table1.poll_id= table2.poll_id');
【解决方案2】:

试试这个。它对我有用!!!

$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');

最好的问候, 图沙尔尼拉斯

更新:

注意:永远不要使用像 $_POST、$_GET 这样的全局数组,直接包含用户的请求数据而不进行安全检查。这可能会导致 SQL 注入等严重问题。

【讨论】:

    猜你喜欢
    • 2012-03-23
    • 1970-01-01
    • 2014-06-22
    • 2011-07-08
    • 1970-01-01
    • 2011-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多