With the following piece of code, how do i know that anything was inserted in to the db?
if ($stmt = $connection->prepare("insert into table (blah) values (?)")) {
$stmt->bind_param("s", $blah);
$stmt->execute();
$stmt->close();
}
I had thought adding the following line would have worked but apparently not.
if($stmt->affected_rows==-1){$updateAdded="N"; echo "failed";}
And then use the $updatedAdded=”N” to then skip other pieces of code further down the page that are dependent on the above insert being successful.
Any ideas?
The
execute()method returns aboolean… so just do this :Update: since 2022 and beyond, a failed query will throw an error Exception. So you won’t have to write any code to "skip other pieces of code further down the page" – it will be skipped automatically. Therefore you shouldn’t add any conditions and just write the code right away:
If you need to do something in case of success, then just do it right away, like
You will see it only if the query was successful. Otherwise it will be the error message.