Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

friend request and show active friends php

Posted on 2011-03-21
4
Medium Priority
?
660 Views
Last Modified: 2012-05-11
i made a working friend request system but it breaks if two users add the same person. :S

its showing users that have been activated for the current memberID in the output of my query
i just put id's of users in both ways in the table when some one wants to add a friend.
but if 2 users add the same person that persons id will show in two rows and my query cant handle that so am trying to fix it so it doesn't have that problem .

the code i am shoing is the bit that checks if the user (when on there profile) has any friends activated and if so show them

 
<?php
	$friendPlayerName = "Player Name: <br>";
$Email = "Players Email: <br>";
$title = "Your Accepted Friends: <br>";

	include("conf.php");
       
    $connection = mysql_connect($databaseURL,$databaseUName,$databasePWord);
        // or die ("Error while connecting to localhost");
    $db = mysql_select_db($databaseName,$connection);
        //or die ("Error while connecting to database");
	
$sqlf = "SELECT friends.friendPage FROM friends 
WHERE friends.activate = '1' AND friends.friendID = '".$_SESSION['MemberID']."'";


$resultf = mysql_query($sqlf)or die(mysql_error());

while($row = mysql_fetch_array($resultf, MYSQL_ASSOC))
{
	$friendPage = $row['friendPage'];//now i can compare this with current log in user later on

}


//where current member id = friendPage and friendId = $friendPage

$sqlfTwo = "SELECT friends.friendID,friends.playername,friends.UserEmail,images.imgSmall FROM friends JOIN images ON images.memberId = friends.friendID 
WHERE friends.activate = '1' AND friendID = '$friendPage'";
$resultfTwo = mysql_query($sqlfTwo)or die(mysql_error());

while($row = mysql_fetch_array($resultfTwo, MYSQL_ASSOC))
{
	

echo '<img src='. $row['imgSmall'].'><br>';
echo '<a href="users.php?id='. $row['friendID']. '">' . $row['playername'] . '</a><br>';
echo $row['UserEmail'].'<br><br>'; 

}

mysql_close($connection);

Open in new window

CREATE TABLE IF NOT EXISTS `friends` (
  `id` int(11) NOT NULL auto_increment,
  `friendID` varchar(50) NOT NULL,
  `UserName` varchar(55) NOT NULL,
  `friendPage` varchar(55) NOT NULL,
  `playername` varchar(55) NOT NULL,
  `activate` varchar(55) NOT NULL,
  `UserEmail` varchar(55) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

--
-- Dumping data for table `friends`
--

INSERT INTO `friends` (`id`, `friendID`, `UserName`, `friendPage`, `playername`, `activate`, `UserEmail`) VALUES
(1, '2', '', '1', 'chris', '1', 'chris@chris.com'),
(2, '1', '', '2', 'admin', '1', 'admin@admin.com'),
(3, '4', '', '1', 'ben', '1', 'ben@ben.com'),
(4, '1', '', '4', 'admin', '1', 'admin@admin.com');

Open in new window


id ,      
friendID = id of session or id of friend (i put this in both ways),
UserName = not using      
friendPage = id of session or id of friend (i put this in both ways),       
0
Comment
Question by:helpchrisplz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 5

Accepted Solution

by:
palanee83 earned 1500 total points
ID: 35186612
My understanding is that,  one person is allowed to add more than one friend. So You need to have separate table to  store the friend request as this is one-to-many relationship.  Design your table as mentioned below to solve your problem.

User Table Schema
===============

CREATE TABLE IF NOT EXISTS `users` (
  `UserID` int(11) NOT NULL auto_increment,
  `UserName` varchar(55) NOT NULL,
  `friendPage` varchar(55) NOT NULL,
  `playername` varchar(55) NOT NULL,
  `activate` varchar(55) NOT NULL,
  `UserEmail` varchar(55) NOT NULL,
  PRIMARY KEY  (`UserID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Friend Table Schema
=================

CREATE TABLE IF NOT EXISTS `friends` (
  `requestID` int(11) NOT NULL auto_increment,
  `UserID` int(11) NOT NULL,
  `friendID` varchar(50) NOT NULL,
  PRIMARY KEY  (`requestID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;





0
 
LVL 1

Author Comment

by:helpchrisplz
ID: 35191175
ok i have them tables up now

do i just put the users in both ways when one adds?

INSERT INTO friends(UserID, friendID) VALUES ('$UserID', '$friendID');

INSERT INTO friends(friendID, UserID) VALUES ('$friendID', '$UserID');


0
 
LVL 1

Author Comment

by:helpchrisplz
ID: 35228046
hmmm my problem was more complicated than i expected
0
 
LVL 1

Author Closing Comment

by:helpchrisplz
ID: 35228048
lots of other stuff that was not working but yes
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question