tags:

views:

47

answers:

2

I was wondering how would I allow a member to accept or deny another member as a friend? How would my PHP & MySQL code look like?

Here is my PHP & MySQL code.

if (isset($_GET['fid'])){
    $friend_id = mysqli_real_escape_string($mysqli, htmlentities(strip_tags($_GET['fid'])));

    if(isset($site_id)){
        $dbc = mysqli_query($mysqli,"SELECT * FROM friends WHERE user_id = '$site_id' AND friend_id = '$friend_id'");

        if(mysqli_num_rows($dbc) == 1){
            echo '<p>Your friend has already been added!</p>';
        } else if(mysqli_num_rows($dbc) == 0){
            $dbc = mysqli_query($mysqli,"INSERT INTO friends (user_id, friend_id, date_created) VALUES ('$site_id', '$friend_id', NOW())");
        }

        if (!$dbc) {
                print mysqli_error($mysqli);
                return;
        }
    }
}

Here is the MySQL table.

CREATE TABLE friends (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
user_id INT UNSIGNED NOT NULL,
friend_id INT UNSIGNED NOT NULL,
date_created DATETIME NOT NULL,
PRIMARY KEY (id),
KEY user_id (user_id),
KEY friend_id (friend_id)
);
A: 

Add another field in your table called "accepted". When someone makes a friend request, set the accepted field to '0' and send a request to the friend. If the friend accepts, update the table and set the accepted field to '1'. If it is 1, then that means they are friends. If 0, then they aren't.

ggfan
How would I go about the PHP code?
alpha
Your mysql table has to be changed and you should set the state or accepted field to "pending" whenever one requests another's friendship by adding a new line to the table. When the other one confirms the friendship, you update the existing db entry by searching for both friends ids.
dhh
How would I display the accept or deny friend request to the member, what type of PHP functions would I need to accomplish this?
alpha
The instructions have been given very clearly above. If you are having trouble using PHP, try [consulting the documentation](http://us2.php.net/docs.php) and then asking *specific questions* about how things work. We give example code here, we won't do your job for you. :)
Charles
@Charles if the answers where clear I wouldn't be asking again :p
alpha
So, where are you getting stuck when implementing the above answer? What *exact, specific* problem are you having? We're glad to help, but we can only help with specific problems. What have you tried in this answer? What isn't working?
Charles
@Charles I'm currently working on it if I get stuck I will post another question :) Thanks for your concern
alpha
A: 

I'd start adding a new field to your mysql table, like state ENUM('pending','approved'). The moment one requests someone's friend state, you set the state to 'pending', when the other user approves the other's friendship the field's set to "approved".

This way you're able to determine if one's friendship was already applied.

dhh
How would I display the accept or deny friend request in my PHP code I get the MySQL table code.
alpha
Hum - please check back with more detailled questions. As the others already stated, please be more precise.
dhh