0

I am trying to delete entries using PDO:

$query = $pdo->prepare("DELETE FROM matches
    WHERE sportsmen_id IN (:sportsmenIds)
    AND game_id = :gameId
    AND category_id = :categoryId
    AND place_id IN (:placeIds)");

$query->bindValue('sportsmenIds', rtrim($sportsmenIds, ','));
$query->bindValue('gameId', $gameId);
$query->bindValue('categoryId', $categoryId);
$query->bindValue('placeIds', rtrim($placeIds, ','));

$query->execute();`

I even tried dumping the query and the values and it resulted correct:

{
     "sportsmenIds": "17117",
      "gameId": 15073,
      "categoryId": 47295,
      "placeIds": "10,38,40,37",
      "query": "DELETE FROM matches WHERE sportsmen_id IN 
          (:sportsmenIds) AND game_id = :gameId AND     
          category_id = 
          :categoryId AND place_id IN (:placeId)"
}

But what's deleted are only entries where place_id = 10 (the first place_id in $placeIds)

I also suspect that if I got more than 1 $sportsmenIds, same thing will also happen, only the first from $sportsmenIds will also be deleted.

What I am expecting:

For all the entries that met the condition of sportsmen_id, game_id, category_id, and all values for place_id inside IN STATEMENT, not just the place_id = 10

nwantiti
  • 41
  • 4
  • You need to define a placeholder for each value inside the array e.g. `[1, 2, 3, ]` would be `WHERE field in (?,?,?)` – DarkBee Jan 12 '23 at 15:10
  • Okay, got it, but another question arise. Why did it not produce any error and proceeds to delete entries that met the condition of first value in IN STATEMENT? – nwantiti Jan 12 '23 at 15:18
  • Cause PDO doesn't throw errors (as exception) by default. See [here](https://stackoverflow.com/questions/32648371/why-does-this-pdo-statement-silently-fail) for more information about this – DarkBee Jan 12 '23 at 15:28

0 Answers0