Jump to content


Photo

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


  • Please log in to reply
11 replies to this topic

#1 pro_se

pro_se
  • Members
  • PipPipPip
  • Advanced Member
  • 131 posts

Posted 23 August 2006 - 08:42 PM

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...
<?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>";
}
}
?>

now i need to figure out how to prevent a user to add a friend twice...

#2 Corona4456

Corona4456
  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts
  • LocationEl Paso, TX

Posted 23 August 2006 - 08:50 PM

you can do one of two things:

  • 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

What is the CoronaMatrix?

#3 pro_se

pro_se
  • Members
  • PipPipPip
  • Advanced Member
  • 131 posts

Posted 23 August 2006 - 09:04 PM

can u give me an example of how to stop the user from adding the friend using the first method?

#4 Corona4456

Corona4456
  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts
  • LocationEl Paso, TX

Posted 23 August 2006 - 09:36 PM

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 :).
What is the CoronaMatrix?

#5 pro_se

pro_se
  • Members
  • PipPipPip
  • Advanced Member
  • 131 posts

Posted 23 August 2006 - 09:49 PM

-- 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 ''
)




#6 Corona4456

Corona4456
  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts
  • LocationEl Paso, TX

Posted 23 August 2006 - 10:05 PM

in your friends table make `userid` and `friendid` a unique set:
ALTER TABLE `friends` ADD UNIQUE `friendid` ( `userid` , `friendid` ) 
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?
What is the CoronaMatrix?

#7 pro_se

pro_se
  • Members
  • PipPipPip
  • Advanced Member
  • 131 posts

Posted 23 August 2006 - 10:15 PM

i dont understand.. :(

#8 Corona4456

Corona4456
  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts
  • LocationEl Paso, TX

Posted 23 August 2006 - 10:19 PM

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.
What is the CoronaMatrix?

#9 pro_se

pro_se
  • Members
  • PipPipPip
  • Advanced Member
  • 131 posts

Posted 23 August 2006 - 10:23 PM

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

#10 Corona4456

Corona4456
  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts
  • LocationEl Paso, TX

Posted 23 August 2006 - 10:33 PM

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.
What is the CoronaMatrix?

#11 pro_se

pro_se
  • Members
  • PipPipPip
  • Advanced Member
  • 131 posts

Posted 23 August 2006 - 10:46 PM

WOW! thanks maan... i changed all the fields to integers... good help! patient too!

#12 Corona4456

Corona4456
  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts
  • LocationEl Paso, TX

Posted 23 August 2006 - 11:38 PM

no problem :).  Glad to help. Let me know if you need help with anything else
What is the CoronaMatrix?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users