Danny Setyawan
Danny Setyawan

Reputation: 85

how to delete data where idin database with codeigniter

i want delete any image in database with id_product I call, but the result just one image, not all image where id_product.

my database :

+------------------------------+
|id_image | id_product | image | 
+------------------------------+
|   1     |   22     | ab.jpg  |
|   2     |   22     | ak.jpg  |
+------------------------------+

my controller :

function produk_dihapus(){
    $id = $this->input->get('id');
    $name = $this->input->get('name');
    $this->produk_adm->hapus_any_image($id);
    log_helper("produk", "Menghapus produk ".$name."");
}

my model :

function hapus_any_image($id){
    $result = array();
    $query = $this->db->get('produk_image',array('id_produk'=>$id));
    foreach ($query->result_array() as $result) 
    {
        //print_r($result);
        unlink('assets/img/produk/'.$result['gambar']);
    }
}

Upvotes: 1

Views: 55

Answers (4)

nmtri
nmtri

Reputation: 474

What about using this:

$ids = array('1', '2', '3');
$this->db->where_in('id_produk', $ids);
$query = $this->db->get('produk_image');

Upvotes: 0

use the following code as its valid in all versions of codeigniter.

$sql = "SELECT * FROM produk_image WHERE id_product = ?";

$this->db->query($sql, array(22));  /* product id = 22 in array*/

Upvotes: 0

JYoThI
JYoThI

Reputation: 12085

There is no method like you passed parameter you can simple apply the where condition or get_where like this

$this->db->where('id_produk',$id);
$query = $this->db->get('produk_image');
$data = $query->result_array();
foreach ($data as $result) 
{
    unlink('assets/img/produk/'.$result['gambar']);
}

Upvotes: 0

Hikmat Sijapati
Hikmat Sijapati

Reputation: 6994

You have to use $this->db->get_where() in the place of $this->db->get() for matching with where condition.So in model

Replace

$query = $this->db->get('produk_image',array('id_produk'=>$id));

With

$query = $this->db->get_where('produk_image',array('id_produk'=>$id));

Upvotes: 1

Related Questions