1

I've viewed a few similar posts regarding this topic, but I haven't found them to work for me.

I have a list of checkboxes that store article ids as a value. This means when the user checks a checkbox, the ids are stored as a string and sent to the handler (something like this: "blah,blah,blah"). I will later delete the ids from the database.

My sql statement looks something like this: "delete from article where art_id in (".$_GET['art_id'].")"

Of course, this doesn't work because $_GET['art_id'] looks like "blah,blah,blah,blah" rather than " 'blah', 'blah', 'blah', 'blah' "

Some answers on the net mentioned spliting the string and using regex. I'm not sure what the best way is. If I wasn't clear on something, please ask and I'll clarify. Much thanks.

2 Answers 2

2

Sanitize, implode and then insert the string:

$ids = array();
$in = '';
if(isset($_GET['art_id']){
  foreach($_GET['art_id'] as $id){
    $ids[] = intval($id);
  }
  $in = implode(',', $ids);
}

$query = "delete from article where art_id in (".$in.")";

You might want to do additional checking in case where $_GET['art_id'] is empty, something like providing a default value or making the query only if you have the IDs

2
  • Thanks, Damien. I figured out the solution to this, but now I'm struggling with a similar problem but with strings. I need "blah,blah,blah" to look like "'blah','blah','blah'". In this case, it's already a string, so implode isn't necessary. And I can't insert an array in the sql statement. If you know how to figure this out, your help would be much appreciated!
    – tickerll
    May 11, 2013 at 7:43
  • That's another question, anyway the principle stands similar May 11, 2013 at 8:00
1

You are looking for implode function

See here

8
  • Thanks, Manu. I made a quick edit to mention that when it gets to the handler, it's a string, so it looks like "blah,blah,blah" before inserting it into the sql statement.
    – tickerll
    May 11, 2013 at 7:20
  • if its and id, shouldn't it be an integer?
    – Manu
    May 11, 2013 at 7:22
  • Yes, it's an integer, and I'm also forcing (int) before inserting it into the sql statement, but when I debug it using var_dump and then die, it shows quotes around it. I concatenated it the way I posted it above, perhaps that's the problem? Also, when I let the handler run (remove die), it only deletes one post rather than all of the ones that were checked.
    – tickerll
    May 11, 2013 at 7:28
  • if your $_GET['art_id'] looks like "1,3,6,7" ( without qoutes ), then it should run. Try printing the value of $_GET['art_id'] before you proceed to execute the query
    – Manu
    May 11, 2013 at 7:31
  • I removed the typecast (int) before the variable and it worked. I didn't realize that forcing (int) returns only one number. Thanks a lot, Manu.
    – tickerll
    May 11, 2013 at 7:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.