samedi 14 novembre 2020

check to see if all returned values of a field = 1

So I have a function to delete a conversation in a custom messaging system. which is as follows

function delete_conversation($conversation_id){
$conn = new mysqli('localhost', 'root', '', 'caspio');
$conversation_id = (int)$conversation_id;

//Select all participants of conversation in question.
$sql = "SELECT DISTINCT conversation_deleted
        FROM conversations_members
        WHERE user_id != {$_SESSION['id']}
        AND conversation_id = {$conversation_id}";
        
$result = mysqli_query($conn, $sql);

return (mysqli_free_result($result) == 1);

//check to see if all other participants have deleted the conversation.
if (mysqli_num_rows($result) === 1 && mysqli_fetch_array($result, MYSQLI_BOTH) == true){
    mysqli_query($conn, "DELETE * FROM conversations WHERE conversation_id = {$conversation_id}");
    mysqli_query($conn, "DELETE * FROM conversations_members WHERE conversation_id = {$conversation_id}");
    mysqli_query($conn, "DELETE * FROM conversations_messages WHERE conversation_id = {$conversation_id}");
} else {
    $sql ="UPDATE conversations_members
            SET conversation_deleted = 1
            WHERE conversation_id = {$conversation_id}
            AND user_id = {$_SESSION['id']}";
            
    $result = $conn->query($sql);
            
}

}

BUT my issue is this

if the conversation has 2 participants or more i.e. my self and one other, or myself and a few others. I want the function to check if all other participants have deleted the conversation before actually removing the data from the data base.

So this is the query to bring up all participants (excluding me) of a certain conversation.

$sql = "SELECT DISTINCT conversation_deleted
            FROM conversations_members
            WHERE user_id != {$_SESSION['id']}
            AND conversation_id = {$conversation_id}";

So say all other participants "conversation_deleted" fields = 1 then delete records

if not all other participants "conversation_deleted" fields = 1 then set my "conversation_deleted" to 1

But for some reason the if statement its not working :(

Aucun commentaire:

Enregistrer un commentaire