Hi all,
I'm trying to delete either 1 record or multiple records (depending on checkbox input, 'recsToDelete').
PHP
$deleteRecsArry = $_POST['recsToDelete']
// Verify post data is legit
$placeHolders = implode(',', array_fill(0, count($deleteRecsArry), '?'));
foreach ($deleteRecsArry as $dr) {
$recsToDelete .= "'".$dr."',";
}
$recsToDelete = rtrim($recsToDelete, ",");
$dSTMT = "DELETE FROM table WHERE Col1 in ($placeHolders) AND Col2 = ? AND Col3 = ?";
$dSTMT = $connection->prepare($dSTMT);
if (!$dSTMT->execute(array( $recsToDelete, $var2, $var3 ))) {
// This doesn't get executed, so doesn't appear to be an issue running the query.
$dbErrArray = print_r($dSTMT->errorInfo(), true);
}
$deleteResult = $dSTMT->rowCount(); // This results in null, or blank (not zero, and not 1).
When I run this, the error is: Warning: PDOStatement::execute() [pdostatement.execute]: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens
However, when I test this by checking two boxes (for example), I can see that that $var2 and $var3 both have data as expected, and $recsToDelete has the two different values from the checkboxes and both are wrapped in single quotes ' and are separated by a comma. Ex: 'a73f0205vtq6','h8n10019O8b4' the IN statement is in (?,?).
Echoing the $dSTMT:
DELETE FROM table WHERE Col1 in (?,?) AND Col2 = ? AND Col3 = ?
So I can see that there are 4 ? placeholders, two for the IN() and two others. The number of data going in to each on the execute pairs up as explained above.
If I check three boxes, $var's 2/3 are both as expected, the $recsToDelete is 'a73f0205vtq6','h8n10019O8b4','4hikc025Wix9' and the IN statement is in (?,?,?)
This looks to me that the placeholders match the number of variables being sent to the execute statement.
What am I missing? As stated in the comment of the execute, it seems to execute the query...however, the records are not deleted. Why doesn't this work?
Thanks.