This does not work this way because the IN() clause expects a set of values, not a comma-separated string, which is what you provide, trying to bind them all as one argument.
To do this, you need to link each item in the collection individually:
// Split the IDs into an array $ids = preg_split('/\s*,\s*/', $_POST['excludeids'], -1, PREG_SPLIT_NO_EMPTY); // Create an array of ? characters the same length as the number of IDs and join // it together with commas, so it can be used in the query string $placeHolders = implode(', ', array_fill(0, count($ids), '?')); // Prepare the statement $STH = $DBH->prepare("SELECT * FROM books WHERE id NOT IN ($placeHolders)"); // Iterate the IDs and bind them // Remember ? placeholders are 1-indexed! foreach ($ids as $index => $value) { $STH->bindValue($index + 1, $value, PDO::PARAM_INT); } // This should now work $STH->execute();
source share