2012-12-25 39 views
3

所在的位置以及条件这是我的代码:更新SQL与笨

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 

当我运行这段代码就删除了我的记录。我不明白为什么。

我希望它: UPDATEWHERE CONDITION_1 CONDITION_2 = B

PS:

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

回答

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(); 

那就试试吧,看看你的记录仍然存在。如果您的更新成功并且您的记录仍在表格中,则可能使用外键使用删除级联。也就是说,如果您从employees表中删除记录,则还将删除dept_emp,salariestitles中的记录。

笨的Active Record类: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?

+0

感谢您的现货。正是这些线条搞乱了它。当外键被删除时,它会删除使用该外键的所有关联记录。感谢您的帮助。 :D –

+0

@ user1306764没问题,很高兴我可以帮忙,如果您觉得我的答案是可以接受的解决方案,请记得点击复选标记。 – PhearOfRayne