0
$ids = $_POST['ids'];

// sky***earth***sea***sun***...

$ids = explode("***", $ids);

foreach ($ids as $id) {
    $st = $db->query("delete from tags where id = " . $id);
}

Is there a more elegant way to delete multiple rows, especially regarding peformances in case of huge array? Something like:

$st = $db->query("delete from tags where id in " . $ids);

Any suggestion?

5
  • 1
    This is a duplicate of stackoverflow.com/questions/17657760/…. On top of that, your code is vulnerable to SQL Injections, please learn about them here: stackoverflow.com/questions/60174/… Commented May 6, 2018 at 17:42
  • @Paul, on client side there is no any input, just user click event. Is the code still vulnerable to sql injeciton? Commented May 6, 2018 at 17:45
  • 2
    Possible duplicate of Better method to delete multiple rows in a MySQL database with PHP? Commented May 6, 2018 at 17:45
  • 1
    @bonaca you are using the superglobal $_POST. There is no need for any form in the frontend. I can just send a POST request to that specific file and my payload in ids will be passed to the query without any filtering, escaping or something related. Commented May 6, 2018 at 17:48
  • 1
    If your id is a text column i.e. sky, earth etc Then the $id needs to be wrapped in quotes like $st = $db->query("delete from tags where id = '$id' "); But do pay heed to the SQL Injection Attack Even if you are escaping inputs, its not safe! Use prepared parameterized statements in either the MYSQLI_ or PDO API's Commented May 6, 2018 at 17:56

2 Answers 2

1

Just replace the exploded *** with a comma(,)

$st = $db->query("delete from tags where id in (" . implode(",", explode("***", $ids)) .")");

Source: https://stackoverflow.com/a/17657893/5837918

Sign up to request clarification or add additional context in comments.

Comments

1

$kk= ''; foreach ($ids as $id) { $kk.=$id. ','; } $kk = rtrim($kk, ','); $st = $db->query("delete from tags where id In($kk))";

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.