问题
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