1

I'm unsure of the direction on how to actually insert a SQL command to create a "friendship" between two users. I have created the tables in PHPMyAdmin I'm at a loss for what to do in order to create a friends list similar to the links below. Let me know if you need more information.

  1. Buddy List: Relational Database Table Design
  2. Facebook database design?

PHP Query:

if(!empty($_POST)) 
{
$searchParams=$_POST['search'];
$appUser = $_POST['username'];

//check if username and or email is already registered
$query = " SELECT 1 FROM Users WHERE username = :user OR email = :email";
//now lets update what :user should be
$query_params = array(
    ':user' => $searchParams,
    ':email'=> $searchParams,
);

try {
    // These two statements run the query against your database table. 
    $stmt   = $db->prepare($query);
    $result = $stmt->execute($query_params);
}
catch (PDOException $ex) 
{

}

//check results of above query and determine if username or email already exists, output results
$row = $stmt->fetch();
if ($row) {

    echo '{"success":2}';

$currentUser = mysqli_result(mysqli_query("SELECT UserID FROM Users WHERE username = $appUser"),0);
$addedUser = mysqli_result(mysqli_query("SELECT UserID FROM Users WHERE username = $searchParams"),0);


   $query2 = "Insert INTO Friends (IDUser,FriendID) VALUES (:username,:friend)"; 

   $query_params = array(
    ':username' => $currentUser,
    ':friend' => $addedUser
    );

 $stmt   = $db->prepare($query2);
 $result = $stmt->execute($query_params);

  echo '{"success":1}';  
}
}
 else
{

}   

My tables are as follows:

User:

enter image description here

Friends:

enter image description here

Current User table data:

enter image description here

Community
  • 1
  • 1
user2793987
  • 199
  • 1
  • 2
  • 15
  • 1
    The answer to the first question you posted seems good to me, and thorough - have you tried following that? – JAL Dec 21 '13 at 05:48
  • The idea is a user is to add a friend's username from a $_POST variable, and the user adding the friend has their username submitted by a $_POST variable. – user2793987 Dec 21 '13 at 06:00
  • The first answer to that question in the link does not explain how to create that relationship in the SQL, just the tables and how to query the table once the data is already set. – user2793987 Dec 21 '13 at 06:09
  • I see... I thought the SQL to create it was evident from the table structure. Like, 'insert into friend_table person_1,person_2 values (person_1_id,person_2_id). – JAL Dec 21 '13 at 14:40

1 Answers1

1

Assuming you have, say, data in your user table like this:

insert into users (iduser, username) values (1, 'amy');
insert into users (iduser, username) values (2, 'bill');
insert into users (iduser, username) values (3, 'chris');

And assuming that you are representing a "friendship" between users via a record in your friends table, you would create a friendship like so:

insert into friends (iduser, friendid) values (1, 2);

This would establish a relationship between user id 1 (amy) and user id 2 (bill).

Then, to subsequently find all the friends of a user, you'd do something like this:

select username from users u
                  inner join friends f on f.friendid = u.iduser
where f.iduser = 1

This would give you the names of all friends of amy.

In response to your comment about dynamically adding friends based on their names, you could do something like this. Here amy is logged into your site, so to simplify the query we will assume that you already have access to her user id of 1.

insert into friends (iduser, friendid)
select 1, iduser from users where username = :friendusername;
Chris Farmer
  • 24,974
  • 34
  • 121
  • 164
  • How would I insert the iduser and friendid dynamically? Say if a user is entering a username in a form that submits the friend's username and adds the record. – user2793987 Dec 21 '13 at 06:02
  • Added a bit about how you could insert the record based on the friend's username. You'd of course want to parameterize that appropriately for user in your php code. – Chris Farmer Dec 21 '13 at 06:31
  • Thanks, but what if her ID is different than 1? – user2793987 Dec 21 '13 at 06:35
  • Could that be dynamically found and inserted to create the relation? – user2793987 Dec 21 '13 at 06:55
  • Here's what I have so far with the query: $query2 = "INSERT INTO Friends (IDUser,FriendID) SELECT 1, IDUser from Users where username=:username OR email=:email"; How would I change 1 to the current logged on user's ID? – user2793987 Dec 21 '13 at 20:49