473,406 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

avoid duplicated friend

hi,
I'mm writing script for adding friend to a friend list.i'm using a form including friend_name (text), message (textarea) and below is my code:
[php]
if(isset($_POST['send']))
{

include('dbconnect.php');

$friend=$_POST['friend2'];
$user=$_COOKIE['username'];


$result=mysql_query("SELECT * FROM friendlist WHERE username='$user'");
$rows=mysql_fetch_array($result);
echo $friend;
$added_friend=$rows['friend'];
echo $added_friend;
$user1=$rows['username'];
echo $user1;

if(($friend==$added_friend)&&($user==$user1))
{
?><script>alert('<? echo $friend; ?> has been on your list already!');
window.location="friendpublic.php";
</script><?
}

else
{
$query="INSERT INTO friendlist (username,friend) VALUES ('$user','$friend')";
mysql_query($query) or die(mysql_error);

?><script>alert('<? echo $friend; ?> has been added to your list!')</script><?

$invite=$_POST['invite'];
$query2="INSERT INTO friend_message (sender,receiver,message,status)
VALUES ('$user','$friend','$invite','1')";
mysql_query($query2) or die(mysql_error);

?><script>alert('A message has been sent to him or her!');
window.location="friendpublic.php";
</script><?

}

}

?>
[/php]

the code [php]if(($friend==$added_friend)&&($user==$user1))[/php] is used to check if the friend was already on the list but somehow it doesn't work. i think the problem is the mysql_fetch_assoc which queries the first row on the friendlist table...

can anyone show me how to fix it?

thanks!
Oct 30 '07 #1
5 1897
code green
1,726 Expert 1GB
You are making hard work of it.
If all you want to do is check if the current applicant is already in the database then change this [PHP]$friend=$_POST['friend2'];
$user=$_COOKIE['username'];


$result=mysql_query("SELECT * FROM friendlist WHERE username='$user'");
$rows=mysql_fetch_array($result);
echo $friend;
$added_friend=$rows['friend'];
echo $added_friend;
$user1=$rows['username'];
echo $user1;

if(($friend==$added_friend)&&($user==$user1))
{
?><script>alert('<? echo $friend; ?> has been on your list already!');[/PHP] To [PHP]
$friend=$_POST['friend2'];
$user=$_COOKIE['username'];
$result=mysql_query("SELECT 1 FROM friendlist WHERE `username`='$user'");
if(mysql_num_rows($result)
?><script>alert('<? echo $friend; ?> has been on your list already!');[/PHP]
Is $user reliable? If it is simply a name typed in a text box it is not.
I assume you are collecting emails so test the email address.
You could also set the field in friendlist to UNIQUE then capture any errors when INSERTing
Oct 30 '07 #2
it doesnot work! I could only add the first friend into my list. for all of these other. the message says they've been added already.
Oct 31 '07 #3
code green
1,726 Expert 1GB
it doesnot work! I could only add the first friend into my list. for all of these other. the message says they've been added already
Please don't use the meaningless
it doesnot work
What others are you talking about?
Can I suggest you break the problem down into smaller steps then we can find where it is going wrong.
Oct 31 '07 #4
Please don't use the meaningless What others are you talking about?
Can I suggest you break the problem down into smaller steps then we can find where it is going wrong.
oh sorry, I'd have made it clearer!
when I use my first code:
[php]
$result=mysql_query("SELECT * FROM friendlist WHERE username='$user'");
$rows=mysql_fetch_array($result);
$added_friend=$rows['friend'];
$user1=$rows['username'];

if(($friend==$added_friend)&&($user==$user1))
{
warning of duplication
}

else
{ ...}
[/php]

I tried to take all of my friend names from my list and compared each of them to one I'm going to add. The problem is the query seems to take only the first name in the table and keep it for any later comparison (say if I add A first then B, C... only later adding A receives warning of duplication, B, C.. do not)

(using while loop for my_fetch_array shouldn't be mentioned: multiple adding for each name^^)

in the second code given by you,
[php]
$result=mysql_query("SELECT 1 FROM friendlist WHERE `username`='$user'");
if(mysql_num_rows($result)
{ warning}
[/php]
things seem even worse: only first friend name added, all others receives warning....

can you help me? thanks!
Oct 31 '07 #5
code green
1,726 Expert 1GB
By this
I tried to take all of my friend names from my list
I assume you mean the `friendlist` table. If so write `friendlist` table and not 'my list'.
Programmers should be precise in their explanation.
and compared each of them to one I'm going to add
There is never a wrong way but you are coming close.
I would stop and have a re-think. You seem to be tackling this the wrong way around.
Are you bulk uploading a list of new friends or only one?
If it is a bulk upload, first load them all into an array,
then loop through the array and query each one seperately
with the `friendlist` table to see if they are there with the query I gave you.
Then you can handle the result however you like for each list item.
If there is only then use the query I gave you.
DO NOT SELECT * FROM `friendlist`, then loop through the entire result set looking for a match.
This is madness.
Oct 31 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Daniel Loose | last post by:
Hi, a friend was so kind to host my php scripts. but when they use phpmailer (or other PHP mail facilities) to email e.g. to an invalid address, the returning error message appears in *my...
14
by: Alexander Malkis | last post by:
A real-life example: int alpha_beta(unsigned depth,Position p, Move& m /*,other args*/) { //...do smth with p if(depth) { Move m; int val=alpha_beta(depth-1,p,m /*,other args*/); } //......
6
by: | last post by:
I have a class with overloading operator new. (Because, if an identical object exists, return a pointer to existed object instead of a new pointer) It has no sense (it is dangerous) to allocate an...
22
by: T.S.Negi | last post by:
Hi All, I want to avoid using cursors and loops in stored procedures. Please suggest alternate solutions with example (if possible). Any suggestion in these regards will be appreciated. ...
10
by: Noozer | last post by:
I'm writing an ASP application and have a noob question... I have a class that access an MS SQL database. I have another class also accesses an MS SQL database and this second class uses objects...
6
by: planetthoughtful | last post by:
Hi All, I have a C# ASP.NET page that submits back to itself to insert details from a form into a database table. When / if the user refreshes the page (and gets the standard warning that POST...
1
by: charliej2001 | last post by:
Hi all My access database has import/export capabiltiy of contact details between outlook. The database is getting big now (1000+ contacts) and so are the outlook address books that have the...
6
by: Ryan Liu | last post by:
Hi, If I have tens of thousands DataRow in a DataTable and allow the end user to pick any DataColumn(s) to check for duplicated lines, the data is so large, is there a better API, algorithm can...
1
by: Ryan Liu | last post by:
Hi, I have multiple C# solutions and they share some utility and generic projects. I put all *.sln files in root directory. File structure is like this( things without ext is a folder): ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.