简体   繁体   中英

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. 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.

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. You are dumping the array on each loop, and you're echoing 'from' each time. 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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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