简体   繁体   English

无法获取要从数据库中删除的消息行

[英]Cannot get message row to delete from database

I'm trying to delete message rows from my database as part of a social networking site for a college project but am getting an error: Severity: Notice Message: Undefined index: id in my view. 我正在尝试从数据库中删除消息行,这是一个大学项目的社交网站的一部分,但出现错误:严重性:通知消息:未定义索引:ID在我的视图中。 Any help much appreciated!!!! 任何帮助,不胜感激!!!

This is my method in the Model:

function delMessage($id)
  {

    $this->db->where(array('id' => $id));
    $this->db->delete('messages');

  }

This is my method in my Controller:

 function delMessage($id) {

    $this->messages->delMessage($id);

    redirect('message');

   }

And this is the code in my view:

 if(!empty($messages)){
           foreach($messages as $message):
           $delete = $message['id']; 
           var_dump($message); ?>
          <li><?=$message['from']?> says...: "<?=$message['message']?>"(<?=anchor("message/delMessage/$delete", 'delete')?>)</li>      
         <?php endforeach?> 
          <?php }else{ ?>

         <?php echo 'No Messages to View'; }?>   

The way you have this written you are calling the view without passing any data to it. 您编写该视图的方式是在调用视图时不传递任何数据。 If you're going to loop through an array in your view, you have to pass the array from your controller. 如果要在视图中循环访问数组,则必须从控制器传递数组。

Your controller needs to look more like the following: 您的控制器需要看起来更像以下内容:

function delMessage($username) {
    $this->load->model('messages_model');
    $data['messages'] = $this->messages->deleteUserMessage($username);
    $this->load->view('message', $data);
}

In order for that to work, your model has to do more than just delete. 为了使它起作用,您的模型必须做的不仅仅是删除。 It also has to query the database and return the array $messages. 它还必须查询数据库并返回数组$ messages。

I could try to help you more, but looking at your code I can't figure out what you're trying to do. 我可以尝试为您提供更多帮助,但是查看您的代码后,我无法确定您要做什么。 You've assigned the variable $delete, but you never used it. 您已经分配了变量$ delete,但从未使用过它。 You are dumping the array on each loop, and you're echoing 'from' each time. 您将在每个循环中转储数组,并且每次都回显“ from”。 At this point, that's like Egyption heiroglyphics. 在这一点上,这就像埃及人的象形文字。 Provide some more info please. 请提供更多信息。

UPDATE BASED ON THE NEW INFORMATION PROVIDED: 根据提供的新信息进行更新:

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM