I have the following simple php code snippet, which will, when called, delete a relevant article from a database. The result is passed to a javacsript function, which will update the page via AJAX. I would like to return the string "false" if the query fails, as I am attempt below.
if($cmd=="deleterec"){
$deleteQuery = "DELETE FROM AUCTIONS1 WHERE ARTICLE_NO = ?";
if ($delRecord = $con->prepare($deleteQuery)) {
$delRecord->bind_param("s", $pk);
$delRecord->execute();
$delRecord->close();
echo "true";
} else {
echo "false";
}
}
I would like to know what I have missed, and the correct way to check if a query was successful or not.
edit:
based on peoples replies, I tried moving the if clause to $delRecord->execute(); instead of prepare, but t his made no difference. I can not get the page to return 'false'.
The below solutions do not work, because the query alwasy executes, even if incorrectly, and hence always returns 'true'. If I try the affected rows method I get the error : Call to undefined method mysqli_stmt::affected_rows()