0

I know it has already been asked, but I'm not able to solve this. Below is my control module

public function ahome()
{
    $this->load->model('model');
    $data['user'] = $this->model->selall('user');
    $this->load->view('ahome', $data);
    if ($this->input->post('del'))
    {
        $did = $this->input->post('chk');

        // print_r($did);

        for ($i = 0; $i < count($did); $i++)
        {
            $multi = $did[$i];
            print_r($multi);
            $this->model->delall("user", $multi);

            // redirect("control/ahome");

        }
    }
}

and this is my model module

 public function delall($tb,$wh)
 {
     $this->db->query("delete from $tb where uid in('$wh')");
 }    

now problem is that it's only delete single row not multiple rows

voytek
  • 2,202
  • 3
  • 28
  • 44
johny
  • 51
  • 1
  • 5
  • 1
    Instead of using `query("delete from $tb where uid in('$wh')");`, you should be using a prepared statement if your DB interface provides the functionality. – Shotgun Ninja Sep 11 '15 at 14:00
  • 1
    However, I think the root problem is in the quotes surrounding `$wh` in your query string; if the element is a string, you're ending up with `where uid in (''strval'')` instead of `where uid in ('strval')`, and if it's a numeric form, then you're turning it into a string. – Shotgun Ninja Sep 11 '15 at 14:02
  • query runs but it takes only one id at a time – johny Sep 11 '15 at 14:03
  • Is the uid column numeric in your database? If so, then you shouldn't have those quotes, because it should be a numeric constant, not a string. Actually, even if it's a string, you probably still shouldn't have those quotes, since it will double-quote the string and cause a syntax error. – Shotgun Ninja Sep 11 '15 at 14:06
  • None of this would matter, though, if you used prepared statements, since they take care of writing the values to the query in the proper format anyway. – Shotgun Ninja Sep 11 '15 at 14:07

2 Answers2

0

In your query string, try removing the quotes around the $wh variable.

public function delall($tb,$wh)
{
  $this->db->query("delete from $tb where uid in ($wh)");
}

That should get it to work; however, you should be using CodeIgniter's query bindings. Something like this:

public function delall($tb,$wh)
{
  $this->db->query("delete from $tb where uid in (?)", array($wh));
}

Refer to this answer for more details.

Community
  • 1
  • 1
Shotgun Ninja
  • 2,540
  • 3
  • 26
  • 32
0

Try this, controller,

public function ahome()
{
    $this->load->model('model');
    $data['user'] = $this->model->selall('user');
    $this->load->view('ahome', $data);
    if ($this->input->post('del'))
    {
        $did = $this->input->post('chk');

        // print_r($did);

        foreach ($did as $d_id)
        {
            $this->model->delall("user", $d_id);

            // redirect("control/ahome");

        }
    }
}

Model:

public function delall($tb,$wh)
 {
     $this->db->where('id',$wh);
     $this->db->delete($tb);
     return true;
 }    
Gayathri
  • 333
  • 1
  • 15