Jump to content

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


pro_se

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...
Link to comment
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]
Link to comment
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 :).
Link to comment
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 ''
)


Link to comment
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?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.