pro_se Posted August 23, 2006 Share Posted August 23, 2006 hey i recently made a post about a friend script.... i got that to work... thanks to everyone that helped out... now i need help to prevent a user to add a friend twice... if you did not read the last post the friend display works as follows... [code]<?php$userid = $_GET['id'];$result = mysql_query("SELECT * FROM friends WHERE userid='$userid' limit 4");while($r=mysql_fetch_array($result)){ $friendid=$r["friendid"]; $result2 = mysql_query("SELECT * FROM users WHERE id='$friendid'");while($r2=mysql_fetch_array($result2)){ $avatar=$r2["avatar"];$name=$r2["name"];echo "<table width=15% height=102 border=0 align=left cellpadding=3 cellspacing=0> <tr> <td height=102 align=center valign=top><a href=user.php?id=$friendid><img src=$avatar width=90 height=90 border=0 /><br /> $name </a></td> </tr></table>";}}?>[/code]now i need to figure out how to prevent a user to add a friend twice... Quote Link to comment Share on other sites More sharing options...
Corona4456 Posted August 23, 2006 Share Posted August 23, 2006 you can do one of two things:[list][*]You can go into mysql and make the friend_id field a unique field... it's easy to do in phpMyAdmin if you have access to it that way I recommend to do it that way.[*]You can do a query first on your friend_id field for the current user. If mysql_num_rows(<query_result>) is > 0 then you can assume the friend has already been added[/list] Quote Link to comment Share on other sites More sharing options...
pro_se Posted August 23, 2006 Author Share Posted August 23, 2006 can u give me an example of how to stop the user from adding the friend using the first method? Quote Link to comment Share on other sites More sharing options...
Corona4456 Posted August 23, 2006 Share Posted August 23, 2006 Ok... well I misread your code the first time so all my references to "friend_id" field is wrong. Anyway... first off ... you can still implement the unique field, however, I don't know the format of your users table nor users table so i can't give you a good example.if you post the format I can easily give you an example though :). Quote Link to comment Share on other sites More sharing options...
pro_se Posted August 23, 2006 Author Share Posted August 23, 2006 -- Table structure for table `users`-- CREATE TABLE `users` ( `status` varchar(255) NOT NULL default 'working', `rel` varchar(255) NOT NULL default '', `orient` varchar(255) NOT NULL default '', `gend` varchar(255) NOT NULL default '', `id` int(10) NOT NULL auto_increment, `name` varchar(20) NOT NULL default '', `username` varchar(40) default NULL, `password` varchar(50) default NULL, `regdate` varchar(20) default NULL, `email` varchar(100) default NULL, `website` varchar(150) default NULL, `location` varchar(150) default NULL, `last_login` varchar(20) default NULL, `fronttext` text NOT NULL, `headline` varchar(50) NOT NULL default 'This is my Headline', `counter` varchar(200) NOT NULL default '1', `avatar` varchar(60) NOT NULL default 'images/user/nopic.gif', `hobbies` varchar(255) NOT NULL default '', `music` varchar(255) NOT NULL default '', `reading` varchar(255) NOT NULL default '', `heroes` varchar(255) NOT NULL default '', `mstatus` varchar(255) NOT NULL default '', `inout` varchar(50) NOT NULL default 'out', `fname` varchar(255) NOT NULL default '', `lname` varchar(255) NOT NULL default '', `showdetails` char(1) NOT NULL default 'n', `showgeneral` char(1) NOT NULL default 'n', PRIMARY KEY (`id`)) -- Table structure for table `friends`-- CREATE TABLE `friends` ( `userid` varchar(255) NOT NULL default '', `friendid` varchar(255) NOT NULL default '') Quote Link to comment Share on other sites More sharing options...
Corona4456 Posted August 23, 2006 Share Posted August 23, 2006 in your friends table make `userid` and `friendid` a unique set:[code]ALTER TABLE `friends` ADD UNIQUE `friendid` ( `userid` , `friendid` ) [/code]running that code will make userid and friendid unique to each other.That way it's one too many both ways. This will prevent the following in your table:userid | friendid---------------------a | ba | bBut you can do: userid | friendid---------------------a | bb | amake sense? Quote Link to comment Share on other sites More sharing options...
pro_se Posted August 23, 2006 Author Share Posted August 23, 2006 i dont understand.. :( Quote Link to comment Share on other sites More sharing options...
Corona4456 Posted August 23, 2006 Share Posted August 23, 2006 hmm... well I'm not sure I can explain it any further than that.However if you run that query. It will solve your problems as to having the user add the same friend twice.if you try to insert a friend twice mysql will give you an error. Quote Link to comment Share on other sites More sharing options...
pro_se Posted August 23, 2006 Author Share Posted August 23, 2006 ErrorSQL query:ALTER TABLE `friends` ADD UNIQUE `friendid` ( `userid` , `friendid` )MySQL said: Documentation#1071 - Specified key was too long. Max key length is 500 Quote Link to comment Share on other sites More sharing options...
Corona4456 Posted August 23, 2006 Share Posted August 23, 2006 ah ok... I see... since you are using varchar(255) this is not possible. Why don't you use integers instead? use the id field instead of the userid field. Quote Link to comment Share on other sites More sharing options...
pro_se Posted August 23, 2006 Author Share Posted August 23, 2006 WOW! thanks maan... i changed all the fields to integers... good help! patient too! Quote Link to comment Share on other sites More sharing options...
Corona4456 Posted August 23, 2006 Share Posted August 23, 2006 no problem :). Glad to help. Let me know if you need help with anything else Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.