【问题标题】:Update SQL with Where AND Where Conditions in codeigniter在 codeigniter 中使用 Where AND Where 条件更新 SQL
【发布时间】:2012-12-11 05:38:31
【问题描述】:

这是我的代码:

public function remove_employee( $emp_no, $now ) {
    $this->db->delete('employees', array('emp_no' => $emp_no));
    $this->db->flush_cache();

    $this->db->start_cache();
    $this->db->where('emp_no', $emp_no);
    $this->db->where('to_date', '9999-01-01');
    $this->db->update('dept_emp', array('to_date' => $now));
    $this->db->stop_cache();
    $this->db->flush_cache();   

    $this->db->start_cache();
    $this->db->where('emp_no', $emp_no);
    $this->db->where('to_date', '9999-01-01');
    $this->db->update('salaries', array('to_date' => $now));
    $this->db->stop_cache();
    $this->db->flush_cache();   

    $this->db->start_cache();
    $this->db->where('emp_no', $emp_no);
    $this->db->where('to_date', '9999-01-01');
    $this->db->update('titles', array('to_date' => $now));
    $this->db->stop_cache();
    $this->db->flush_cache();   

    return;
} //END REMOVE EMPLOYEE

当我运行此代码时,它删除了我的记录。我不明白为什么。

我希望它: 更新WHERE CONDITION_1 AND CONDITION_2 = B

PS:

**$now** is todays date (i.e. 2012-12-25)
**$emp_no** is a unique employee number i.e. 500122

【问题讨论】:

    标签: mysql sql codeigniter activerecord


    【解决方案1】:

    调试此问题的可能方法是注释掉$this->db->delete()

    尝试更改这些行:

    $this->db->delete('employees', array('emp_no' => $emp_no));
    $this->db->flush_cache();
    

    收件人:

    //$this->db->delete('employees', array('emp_no' => $emp_no));
    //$this->db->flush_cache();
    

    然后试试看你的记录是否还在。如果您的更新成功并且您的记录仍在表格中,则您可能有一个使用 Delete Cascade外键。这意味着,如果您从employees 表中删除记录,您也将删除来自dept_empsalariestitles 的记录。

    CodeIgniter 活动记录类:http://ellislab.com/codeigniter/user-guide/database/active_record.html

    InnoDB 外键:http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html

    检查外键:How do I see all foreign keys to a table or column?

    【讨论】:

    • 感谢您的关注。正是这些台词搞砸了。当外键被删除时,它会同时删除所有使用该外键的关联记录。谢谢您的帮助。 :D
    • @user1306764 没问题很高兴我能提供帮助,如果您认为我的回答是可以接受的解决方案,请记得点击复选标记。
    猜你喜欢
    • 1970-01-01
    • 2016-11-18
    • 2020-06-11
    • 2020-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    • 2015-03-08
    相关资源
    最近更新 更多