I have this class User with a method inside it called deleteFriend. This uses a query using PDO to delete a row in the database table.
$db = new PDO('mysql:host=localhost;dbname=goal;charset=utf8','root','');
$auth_id = 5;
$user_user_id = 6;
echo User::deleteFriend($auth_id, $user_user_id, $db);
echo User::deleteFriend($user_user_id, $auth_id, $db);
As you can see this method is called 2 ways. Delete when auth_id is 5 or 6. And when user_user_id is 6 or 5. Below is the query which has the added parameter that accepted must be 1.
public function deleteFriend($auth_id, $user_id, $db){
$stmt = $db->prepare("DELETE FROM `friends` WHERE `user_id` = ? AND `friend_id` = ? AND `accepted` = '1'");
$stmt->bindParam(1, $auth_id);
$stmt->bindParam(2, $user_id);
if($stmt->execute()){
return 1;
}else{
return 2;
}
}
Now heres that part that interests me. Below you can see I have 2 rows. One with a user_id of 6 and another of 5. And the other row has a friend_id of 5 and the other 6.
When these methods are run with these values they both return 1. Even though the method that refers to the row with accepted of 0 does not delete and the one that has accepted of 1 does delete. My guess here is that the $stmt->execute() is returning true for the method that has accepted of 0. But why? It isn't satisfying the criteria for the query because accepted does not = 1. The functions work as they are supposed to and for my use it dosn't matter if it returns 1 or 2 because the end result is the same (it only deletes the correct row with accepted of 1) but I am curious as to what the reason for this is. If anyone can shed some light on this I'd really appreciate it. And apologizes if my explanation of the question is confusing. I am confused myself. :-(