-1

I am trying to build a simple delete function in my project to delete data from a table. I .can't figure out why it is not working. I used the codeigniter user guide to help me out with this. I am getting an error and don't know why. Here is my controller, model and view for the delete:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: stdClass::$id

Model

function delete()
{
    $this->load->database();
    $tables = array('info', 'info2');
    $this->db->where('id', 1);
    $this->db->delete($tables);

}

controller

 function del($tables){


if((int)$tables > 0){
          $this->info_model->delete($tables);
}

$data = $this->info_model->delete();
$data['query'] = $this->result_model->delete();

$this->load->view('info_view',$data);    
 }

view

//this line gives me the error

 <td><?php echo anchor('info_controller/del' . $row->id,'Delete')?> </td>

EDIT

I used this tutorial to make this

http://www.phpeveryday.com/articles/CodeIgniter-Form-Centralizing-$data-P291.html
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user2143150
  • 71
  • 1
  • 8

2 Answers2

3

In your controller Load your model like this

$this->load->model('deletion_model');

Then call the method of the deletion_model

$this->deletion_model->delete($id);

Make sure you load the model.

after that i suggest you pass value of id as string.

$this->db->where('id', '1');

OR

use the variable supplied to this function if you are not using static values

$this->db->where('id', $id);

Please read the guide properly

http://ellislab.com/codeigniter/user-guide/database/active_record.html#delete

You can also retrieve the id of the record for deletion by using

$id=$this->uri->segment(3); // depending on which segment the id is
Muhammad Nasir
  • 1,796
  • 1
  • 14
  • 22
-1
  1. I do not see any $row object in your controller, although you use it in your view: $row->id
  2. Your model is not returning anything.
  3. If you delete a specified row, $object->id will cease to exist.
  4. The link in your view it states that you want to show a delete link, but you use that view in the controller that deals with the actual delete action.

Edit: Because you try to iterate in your view through query results, $data['query'] from your controller should point to a select statement.

Alexandru Guzinschi
  • 5,675
  • 1
  • 29
  • 40
  • i do have in my view forgot to include that – user2143150 Mar 12 '13 at 10:45
  • Explaining the down vote it would be nice. @user2143150 see the updated answer. – Alexandru Guzinschi Mar 12 '13 at 10:51
  • tnx for your help but i am still not understanding can u use more examples – user2143150 Mar 12 '13 at 11:10
  • @user2143150 In your controller assign query results from select statemment to `$data['query']` like `$data['query'] = $this->db->get('info')` so that in view you can iterate with `foreach($query as $row)`. As a side note: If you do not understand something from an answer, you do not down vote, you just request for clarification. – Alexandru Guzinschi Mar 12 '13 at 11:18