我正在尝试使用MVC Codeigniter框架从我的数据库中的“messages”表中删除消息,这是我为大学创建的社交网站。我的db-id,to,from和message中的“messages”表中有4个字段。我遇到了麻烦 - 代码正在删除“messages”表中的所有行。我只想一次删除一行(消息)。虽然所有消息都被删除 - 它会抛出一个错误:消息:未定义的变量:用户名 - 我应该通过“用户名”吗? - 非常感谢任何帮助。
This is the function in my Controller:
function delMessage($username) {
$this->load->model('messages');
$data['messages'] = $this->messages->deleteUserMessage($username);
$this->load->view('message', $data);
}
This is the function in my Model:
function deleteUserMessage($username) {
$this->db->where('from', $username);
$this->db->or_where('to', $username);
$query = $this->db->get('messages');
if ($query->num_rows() > 0){
$messages = $query->result_array();
$this->db->where('from', $username);
$this->db->or_where('to', $username);
$this->db->delete('messages');
return $messages;
}
}
This is the code in my view:
<?php foreach($messages as $message): ?>
<li><?=$message['from']?> says...: "<?=$message['message']?>"<br/><?=anchor("message/delMessage/$username", 'Delete Message')?></li>
<?php endforeach?>
答案 0 :(得分:0)
你的模型应该是这样的,
function deleteUserMessage($username) {
$this->db->where('from', $username);
$this->db->or_where('to', $username);
$query = $this->db->delete('messages');
if($query) {
return true;
}
}