删除mysql记录在codeIgniter中不起作用


Delete mysql records is not working in codeIgniter

这是我的控制器功能:

function delete_article($title){
    if ($this->session->userdata('User') && $this->session->userdata('User') == 'admin@example.com') {
        $this->load->model('Article', 'Article', TRUE);
        $this->Article->delete_article_db($title);
        redirect('admin/user_article');
    } else {
        redirect('');
    }
}

这是我删除数据库记录的模型函数:

 function delete_article_db($title) {
    $this->db->where('Title', $title);
    $this->db->delete('article');
}

当我运行此代码时,不会删除任何内容。但是,该代码不会引发任何错误或警告。

这是我的MySQL表结构:

CREATE TABLE IF NOT EXISTS `article` (
   `Name` text NOT NULL,
   `Email` text NOT NULL,
   `Phone` text NOT NULL,
   `Address` text NOT NULL,
   `Literature` text NOT NULL,
   `Title` text NOT NULL,
   `Submission_Name` text NOT NULL,
   `Additional_Name` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

有点猜测,但看看你的控制器方法和变量名,我假设你通过url传递标题,类似

http://example.com/admin/delete/Title to be deleted

这让我认为您的查询不起作用,因为url中的空格(或其他字符)的编码与数据库中未编码的空格不匹配。

试用:

function delete_article_db($title) {
    $this->db->where('Title', rawurldecode($title) );
    $this->db->delete('article');
}