Solved

friend request and show active friends php

Posted on 2011-03-21
4
647 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
  • 3
4 Comments
 
LVL 5

Accepted Solution

by:
palanee83 earned 500 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

831 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