Reputation: 3574
I'd like to get notified when a movie does not exists. This represents a script to delete movies from the database. This is a part of what I have now:
$title = $_POST["title"];
$delMovie = mysqli_prepare($link, "DELETE FROM movie WHERE title=?");
mysqli_stmt_bind_param($delMovie, "s", $title);
if (mysqli_stmt_execute($delMovie)) {
echo("Movie \"" . $title . "\" successfully deleted: ");
echo(mysqli_stmt_affected_rows($delMovie) . " rows affected");
} else {
echo("Error (" . mysqli_stmt_errno($delMovie) . "): ");
echo(mysqli_stmt_error($delMovie));
}
mysqli_stmt_free_result($delMovie);
mysqli_stmt_close($delMovie);
mysqli_close($link);
When the movie does not exists, it still returns:
Film "Movie1" successfully deleted: 0 rows affected
As you can see, it doesn't exist, but still returns "Successfully". How can I change this to show:
Error: Movie does not exists in database
Upvotes: 0
Views: 623
Reputation: 496
How about the following:
if (mysqli_stmt_affected_rows($delMovie) == 0) then
echo "Error: Movie does not exists in database";
Just because mysql returns 0, doesn't mean you have to show the result as if it's more than one.
=============================================
You're only other option would be to
1) Do a select statement to check for existence
2) If not exists
a) Show error
b) Delete
- If error deleting, show error
- Else show success
or
1) Delete
- If 0 rows affected or error, Show error message
- Else show success
I think a variation on the second path makes for cleaner code.
Upvotes: 2