Delete mysql records is not working in codeIgniter

£可爱£侵袭症+ 提交于 2019-12-24 09:26:05

问题


This is my controller function:

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

And this is my model function for deleting the database record:

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

When I run this code, nothing gets deleted. However, the code does not fire any errors or warnings.

This is my MySQL table structure:

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;

回答1:


A bit of a wild guess, but looking at your controller method, and the variable names, I'm assuming you're passing the title via url, something like

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

Which leads me to think your query is not working because of encoding of the spaces in the url (or other characters) which won't match the not encoded spaces in your db.

Try with:

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


来源:https://stackoverflow.com/questions/20224362/delete-mysql-records-is-not-working-in-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!