Welcome Guest, Not a member yet? Register   Sign In
Session->set_flashdata Doesn't Work After Database Query
#1

[eluser]Nalorin[/eluser]
Ok, so this is really strange:

I am working on a segment of my project where the user deletes an ad that they have created.

The following is the code that I have currently:
Code:
function delete_ad()
{
  // Check if any checkboxes are selected
  if ( ! $this->input->post('deletable'))
  {
    $this->session->set_flashdata('error_message','You did not select anything to delete!');
    redirect('member/my_ads');
  }
  // Check if the user confirmed they want to delete
  else if ( ! $this->input->post('yes'))
  {
    $this->session->set_flashdata('error_message','You did not confirm that you wanted to delete the selected ads. Please try again.');
    redirect('member/my_ads');
  }
  // Delete the selected ads
  else
  {
    $deletables = $this->input->post('deletable');
    
    $this->load->model('ad_model');
    
// If the offending line below is placed here, instead, it has no problem setting the flashdata
    foreach ($deletables as $key => $deletable)
    {
      list($table,$id) = explode(':',$deletable);
      
      if (($success = $this->ad_model->delete_record($table,$id)) === FALSE)
      {
        show_custom_error('Database Error','DB267_delete_ad');
      }
      else if ($success === 0)
      {
        show_custom_error('Invalid Selection','Could not delete the selected ads because the selection was invalid. Please '.anchor('member/my_ads','Go Back').', refresh the page, and try again. If you receive this error message again, please '. anchor('more/contact_us/report','Report this Error').'.');
      }
    }
    
// THIS LINE is the one causing problems
    $this->session->set_flashdata('success_message','The selected ads were successfully deleted.');
    redirect('member/my_ads');
  }
}
First off, a note: the $deletables variable is an array that stores the values of the checkboxes from the ad management page. They are NVP's of table:id so that I know which table to delete from and which record to delete.

Now, the problem is that the line indicated in the code above does not work where it's placed. However, if I place it above the foreach loop (where indicated), it works fine. The problem with the latter option is that none of the work has been done, yet, so the message would not reflect the true status of the work.

The delete_record function returns TRUE on success, FALSE on failure, and 0 if an invalid table is selected (so the user cannot delete from whatever table they want by tampering with the input.

I am getting a database error:
Code:
Error Number: 1054

Unknown column 'user_id' in 'where clause'

UPDATE `ci_sessions` SET `last_activity` = 1283913044, `user_data` = 'a:2:{s:10:\"login_data\";a:5:{s:2:\"id\";s:1:\"1\";s:5:\"cname\";s:10:\"Neil Smith\";s:5:\"email\";s:17:\"[email protected]\";s:5:\"phone\";s:10:\"4038922272\";s:9:\"logged_in\";s:1:\"1\";}s:25:\"flash:new:success_message\";s:43:\"The selected ads were successfully deleted.\";}' WHERE `user_id` = '1' AND `user_id` = '1' AND `session_id` = 'cb01d631587855816c4e2aa2f0170667'

Can someone please point out where I'm going wrong?
Thanks!
#2

[eluser]Nalorin[/eluser]
Hrmm... if nobody's responding, I wonder if this is a bug?
#3

[eluser]Nalorin[/eluser]
Ok - I found the solution. It had to do with overlapping Active Record calls.

See, I had a bunch of items in my database, and was deleting them while I was testing this form... and I was running out of things to delete (so I worked in favour of "pretending" to delete ads, rather than having to repopulate the database)... My code in my Ad_model looked like this:

Code:
function delete_record($table,$id)
  {
    if ( ! in_array($table,array('tbl1','tbl2','tbl3'))) return 0;

    $login_data = $this->session->userdata('login_data');

    // The following 2 lines were the problem - I was setting an active record "where" clause
    // but never using it, so it poisoned the session model's active record query.
    $this->db->where('user_id',$login_data['id']);
    return TRUE; // inserted for debugging
    // when I flipped the above two statements around, it fixed the problem, since the where
    // clause was not getting executed anymore

    return $this->db->delete($table,array('id'=>$id));
  }
#4

[eluser]InsiteFX[/eluser]
You can also use keep_flashdata for another round!

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB