I am attempting to remove multiple private messages from the database by choosing multiple checkboxes within the mailbox and clicking undergo remove. I've the code below but nothing happens. I am unsure what I am missing..


<?php echo form_open('pm/remove_checked'); ?>

<?php foreach ($query as $row): ?>   
    <input type="checkbox" name="msg[]" value="<?php echo $row->id; ?>" />
    <?php echo $row->from; ?>
    <?php echo $row->subject; ?>    
    <?php echo date("m/d/Y",strtotime($row->msg_date)); ?>          
<?php endforeach; ?>

<?php echo form_submit('delete', 'Delete'); ?>



function remove_checked()
        //validation rules
        $this->form_validation->set_rules('msg[]', 'Private Message', 'required|xss_clean'); 

        if ($this->form_validation->run() == FALSE) 
            $data['query'] = $this->Pm_model->received_msg(); 
            $this->load->view('pm/inbox', $data);   
        else //success
            $checked_messages = $this->input->post('msg'); //selected messages

            //redirect to inbox                                     



function delete_checked($checked_messages) 
    $checked_messages = array();
    foreach ($checked_messages as $msg_id):

          $this->db->where('id', $msg_id);
          $query = $this->db->get();

          if ($query->num_rows() > 0) //if message exists
            $this->db->where('id', $msg_id);
            $this->db->where('recipient', $this->users->get_user_id()); //verify if recipient id is equal to logged in user id

                return FALSE;


You're setting $checked_msg towards the inputs which are checked, however you're passing another variable known as $checked_messages towards the model. You will want to pass the $checked_msg var towards the model.

Also, inside your model, you're redeclaring the $checked_messages var and setting it for an empty array. You will need to remove that otherwise it'll overwrite the data you're passing towards the method.

Inside your current delete_checked() method, you're returning FALSE the moment the very first message is "found" that does not exist, this can avoid the relaxation from the messages from being erased as return stop execution from the loop. If you wish to get it done by doing this, use continue rather and think about using transactions.

If you do not particularly worry about producing individual errors for every message, your model function could be simplified a little:

function delete_checked($message_ids) 
        ->where_in('id', $message_ids)
        ->where('recipient', $this->users->get_user_id())
    return $this->db->affected_rows() > 0;

This can just make an effort to remove the records. When they don't exist they'll be overlooked, and $this->db->affected_rows() should return the amount of messages erased. You are able to compare it to count($message_ids) if you wish to make sure that all messages selected were erased, or make use of this example method that only inspections if a minumum of one message was erased. When the message does not exist, you don't have to remove it anyways.

All the stuff Chris Schmitz mentioned is correct and essential as well, you've some very fundamental errors. You might want to cast to array rather than setting $checked_messages for an empty array should you expect you might be passing just one id (integer or string) for this function. Such as this:

$message_ids = (array) $message_ids;