mysql - 在 codeigniter 中使用 Where 和 Where 条件更新 SQL

标签 mysql sql codeigniter activerecord

这是我的代码:

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

附言:

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

最佳答案

可能的调试方法是注释掉 $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();

那就试试吧,看看你的记录还在不在。如果您的更新成功并且您的记录仍在表中,则您可能有一个使用删除级联外键。这意味着如果您从 employees 中删除记录表,您还将删除 dept_emp 中的记录, salariestitles .

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?

关于mysql - 在 codeigniter 中使用 Where 和 Where 条件更新 SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14027432/

相关文章:

php - PDO 错误 : "Invalid parameter number: parameter was not defined"

php - 无法插入 ActiveRecord Yii2 中的数据库

java - 检索表时无法获取表中 0 位置的第一个元素?

php - mysql多表查询返回排序结果发送查看之前

php - 复制整个 MySql 数据库

sql - 如何将时间戳从一个范围扩展到另一个范围?

带有if条件的mysql过程

php - 像使用一张表一样使用两张表

jquery - 如何将 Bootstrap 添加到按钮

php - regex_match 的表单验证规则