PapT
PapT

Reputation: 623

Deleting content in Codeigniter

I'm building a blog with Codeigniter and I'm having trouble deleting categories. I can create them, edit them, but I can't remove them. I get this error:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: data

Filename: models/article_model.php

Line Number: 167

Backtrace:

File: F:\wamp64\www\site\application\models\article_model.php Line: 167 Function: _error_handler

File: F:\wamp64\www\site\application\controllers\admin\categories.php Line: 85 Function: delete_category

File: F:\wamp64\www\site\index.php Line: 315 Function: require_once

A Database Error Occurred

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IS NULL' at line 3

DELETE FROM `categories` WHERE `id` = '5' AND IS NULL

Filename: F:/wamp64/www/site/system/database/DB_driver.php

Line Number: 691

I'm using PHP7 and CodeIngiter 3.1.3

Here is the code from my article_model.php

public function delete_category($id){
    $this->db->where('id', $id);
    $this->db->delete('categories', $data); // this is line 167
    return true;
}

Here is the code from categories.php

public function delete($id){
    $this->Article_model->delete_category($id); // this is line 85

    //Create Message
    $this->session->set_flashdata('category_deleted', 'Your category has been deleted');

    //Redirect to articles
    redirect('admin/categories');
}

Any help is appreciated, thanks

Upvotes: 0

Views: 48

Answers (1)

Vickel
Vickel

Reputation: 7997

just do it like this

$this->db->delete('categories', array('id' => $id)); 

you could also write it as:

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

the $data array is needed for inserts or updates only, for deleting you just use e.g. the id of the row in question

Upvotes: 2

Related Questions