0

I want delete several row together with foreach, but i have this output bool(false) not array from following code. how is fix it?

<input type="checkbox" name="checked[]" value="1">

function delete_test()
{
    $delete = $this->input->post('checked');
    if (is_array($delete) && count($delete) > 0) {
        foreach ($delete as $val) {
            $this->db->query("DELETE FROM hotel_units WHERE relation LIKE '$val'");
        }
        var_dump($delete);
        echo "<br>is array";
    } else {
        var_dump($delete);
        echo "<br>not array";
    } // This output is: bool(false) not array
}
Jennifer Anthony
  • 2,227
  • 10
  • 37
  • 56
  • Please read: [Best way to stop SQL Injection in PHP](http://stackoverflow.com/questions/60174/best-way-to-stop-sql-injection-in-php) (as long as you don't ensure your query can be valid, you can not expect it to run as intended) – hakre Oct 05 '11 at 12:55
  • why don't you use print rather than var_dump in else condition? – Nimit Dudani Oct 05 '11 at 12:55
  • have you tried: $delete = $this->input->post['checked']; – SW4 Oct 05 '11 at 12:56
  • Check `var_dump($_POST)` when you have actually checked some of the boxes. Also beware this code is wide open to SQL injection. You don't perform any validation on `$val` – Michael Berkowski Oct 05 '11 at 12:56
  • Verify that you have a post variable named `checked` in your `input`, see http://codeigniter.com/user_guide/libraries/input.html – hakre Oct 05 '11 at 12:56

1 Answers1

0

I can only guess what $this->input->post means, but if your checkbox is called 'checked[]' then that is what you need to get from the $POST:

$this->input->post('checked[]');
Colin Fine
  • 3,334
  • 1
  • 20
  • 32