Jump to content

Archived

This topic is now archived and is closed to further replies.

pro_se

**solved ** see if row exists ** solved **

Recommended Posts

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...

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
can u give me an example of how to stop the user from adding the friend using the first method?

Share this post


Link to post
Share on other sites
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 :).

Share this post


Link to post
Share on other sites
-- 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 ''
)


Share this post


Link to post
Share on other sites
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          |              b
a          |              b

But you can do:

userid    |    friendid
---------------------
a          |              b
b          |              a

make sense?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Error

SQL query:

ALTER TABLE `friends` ADD UNIQUE `friendid` ( `userid` , `friendid` )

MySQL said: Documentation
#1071 - Specified key was too long. Max key length is 500

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
WOW! thanks maan... i changed all the fields to integers... good help! patient too!

Share this post


Link to post
Share on other sites
no problem :).  Glad to help. Let me know if you need help with anything else

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.