RyanUK
RyanUK

Reputation: 67

Deleting a database record using codeigniter

Ok, Struggling abit here, I'm pretty new to codeigniter and quite new to PHP. I want to delete a record from the DB. I'm getting an error with the controller. Here is my code

Controller:

    public function removeitem(){
    $this->load->helper(array('form', 'url'));
    $this->load->view('vRemove');
}

public function doremove(){
    $this->load->model("mphoto");

    $this->mphoto->removeItem($id);
    $this->load->view('removed_success');
}

Model:

    public function removeItem($id){
    $this->db->where('ProductID', $id);
    $this->db->delete('Streamline');
}        

and View:

    <?php echo form_open_multipart('site/doremove');?>

    <p>Are you sure you want to remove this item?</p>

    <a href="<?php echo base_url();?>index.php/site/admin">
      <input type="button" value="Cancel" />
    </a>
    <div><input type="submit" value="Submit" /></div>
    </form>
    <?php  ?>

Error I'm getting is:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: id

Filename: controllers/site.php

Line Number: 114

Which is this line:

    $this->mphoto->removeItem($id);

EDIT: The error no longer exists after changing the following code in the controller but the problem now is that the item from the database is not being deleted.

    public function doremove(){
    $this->load->model("mphoto");
    $id = $this->input->get('ProductID');
    $this->mphoto->removeItem($id);
    $this->load->view('removed_success');
}

Any help is greatly appreciated.

Upvotes: 2

Views: 1425

Answers (2)

jleft
jleft

Reputation: 3457

$id in the controller's doremove() function is undefined. You need to pass a the value of the ID from the view to the controller; there are a couple of common ways to achieve this.

URI segment parameter (GET)

Pass the ID through a URI segment. For example: http://yousite.com/site/doremove/2 would pass 2 as the parameter to the doremove function in the site controller.

View

<p>Are you sure you want to remove this item?</p>
// Change 2 to the id of the product you want to remove - you can do this dynamically
<a href="echo site_url('site/doremove/2');">Yes</a>
// Add cancel button etc...

Controller

public function doremove($id = null) {
    if ($id === null)
    {
        // The ID isn't set - show an appropriate message, redirect, whatever you want...
    }
    else
    {
        $this->load->model("mphoto");

        // Is the delete operation sucessful?
        if ($this->mphoto->removeItem($id))
            $this->load->view('removed_success');
        else
            $this->load->view('removed_failure'); // Or whatever you want
    }
}

Through a form (POST)

View

<?php echo form_open('site/doremove');?>
  <p>Are you sure you want to remove this item?</p>
  // Change 2 to the id of the product you want to remove
  <input type="hidden" name="product_id" value="2" />
  <input type="submit" value="Submit" />
</form>

Controller

public function doremove() {
    $id = $this->input->post('product_id');
    if($id)
    {
        $this->load->model("mphoto");    

        // Is the delete operation sucessful?
        if ($this->mphoto->removeItem($id))
            $this->load->view('removed_success');
        else
            $this->load->view('removed_failure'); // Or whatever you want
    }
    else
    {
         // The ID isn't set - show an appropriate message, redirect, whatever you want...
    }

}

It'd also be a good idea to check if the action performed on the database is successful.

Model

public function removeItem($id) {
    // Attempt to delete the row
    $this->db->where('ProductID', $id);
    $this->db->delete('Streamline');
    // Was the row deleted?
    if ($this->db->affected_rows() == 1)
        return TRUE;
    else
        return FALSE;
}

Upvotes: 3

Guilherme
Guilherme

Reputation: 1990

In the function doremove() $id is undefined, you need to take the $id of the resource

If the HTTP method is GET, you need to use the $id = $this->input->get('the_input_name');

or if is POST, $id = $this->input->post('the_input_name');

or you can use a user friendly way, passing the $id in the function scope, doremove($id), just make shure your URL is set properly ('site/doremove/$id').

Read the User Guide, it will make a big difference in your code. In this link have a good example of a simple form using codeigniter: http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html

Upvotes: 1

Related Questions