$5 paypal to first fixer,simple; How to see if someone already added that friend

Discussion in 'PHP' started by donthate, Jul 14, 2007.

  1. #1
    Alright, i have a seperate table in my DB called: friends

    It has 3 rows

    1. id ( incremented row id .. nothing special )
    2. user ( the user who added the other user as their friend )
    3. myfriend ( the friend of user that they added )

    Right now in the add friend file, all i have for adding the friend ( works fine ) is:

    mysql_query("INSERT INTO friends 
    (user, myfriend) VALUES('$uid', '$mem' ) ") 
    or die(mysql_error());
    
    echo "<b><center><font size='4'><u>$mem</u> is now your friend!</font></u></center></b><br>";
    
    echo "<br><br><center><b><font size='5'><a href='member.php?msg=added'>Back to Member Area</a></font></b></center><br><meta http-equiv='refresh' content='3;url=http://www.**********/member.php?msg=added'>";
    
    
    Code (markup):
    But i need it to check if user has already added myfriend , and if they have, don't add them again and echo a message saying " $mem is already your friend! "

    So, it checks if " user " has added " myfriend " before in the DB, and if they have, it wont show/do the insert into friends , and instead echo the message saying " $mem is already your friend! " error.

    $5 paypal to the first person who get's me a working code that i try and am happy with!
     
    donthate, Jul 14, 2007 IP
  2. Bishop81

    Bishop81 Peon

    Messages:
    757
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I'm not a MySQL expert, so my way may not be the best, but it should work.

    
    $result = mysql_query("SELECT FROM friends WHERE user='$uid' AND myfriend='$mem') 
    if (!$result)
    echo "Query failed!" . mysql_error();
    else
    
    if ($row = mysql_fetch_array($result)) {
      echo("$mem is already your friend");
    } else {
    mysql_query("INSERT INTO friends 
    (user, myfriend) VALUES('$uid', '$mem' ) ") 
    or die(mysql_error());
    
    echo "<b><center><font size='4'><u>$mem</u> is now your friend!</font></u></center></b><br>";
    }
    
    Code (markup):
    I'll check and verify that this works, but go ahead and try it out.

    If it does, my paypal is :D
     
    Bishop81, Jul 14, 2007 IP
  3. donthate

    donthate Banned

    Messages:
    922
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Im getting:
    Parse error: syntax error, unexpected T_STRING in /home/ffilehos/public_html/***/maddfriend.php on line 117

    Which is:

    116: if (!$result)
    117: echo "Query failed!" . mysql_error();
    118: else
     
    donthate, Jul 14, 2007 IP
  4. Bishop81

    Bishop81 Peon

    Messages:
    757
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Just remove those 3 lines and see how it goes. They aren't awfully important, just a test to make sure that the query works first. Let me know how it works after that.
     
    Bishop81, Jul 14, 2007 IP
  5. donthate

    donthate Banned

    Messages:
    922
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Parse error: syntax error, unexpected T_VARIABLE in /home/ffilehos/public_html/*****/maddfriend.php on line 118
     
    donthate, Jul 14, 2007 IP
  6. donthate

    donthate Banned

    Messages:
    922
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #6
    117: if ($row = mysql_fetch_array($result)) {
    118: echo("$mem is already your friend");
    119: } else {
     
    donthate, Jul 14, 2007 IP
  7. Bishop81

    Bishop81 Peon

    Messages:
    757
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Check and make sure that there is a ";" at the end of each line... I think I missed one.
     
    Bishop81, Jul 14, 2007 IP
  8. donthate

    donthate Banned

    Messages:
    922
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Which lines ? Im using this below, removed the 3 lines you said:

    $result = mysql_query("SELECT FROM friends WHERE user='$uid' AND myfriend='$mem')

    if ($row = mysql_fetch_array($result)) {
    echo("$mem is already your friend");
    } else {
    mysql_query("INSERT INTO friends
    (user, myfriend) VALUES('$uid', '$mem' ) ")
    or die(mysql_error());

    echo "<b><center><font size='4'><u>$mem</u> is now your friend!</font></u></center></b><br>";
     
    donthate, Jul 14, 2007 IP
  9. Bishop81

    Bishop81 Peon

    Messages:
    757
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Just that first line was missing it.
     
    Bishop81, Jul 14, 2007 IP
  10. Bishop81

    Bishop81 Peon

    Messages:
    757
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Wait, also need to add something else.

    $result = mysql_query("SELECT FROM friends WHERE user='$uid' AND myfriend='$mem'");

    I forgot the double quote at the end.
     
    Bishop81, Jul 14, 2007 IP
  11. donthate

    donthate Banned

    Messages:
    922
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Still:

    Parse error: syntax error, unexpected T_VARIABLE in /home/ffilehos/public_html/insanetext/maddfriend.php on line 118

    w/o the 3 lines
     
    donthate, Jul 14, 2007 IP
  12. Bishop81

    Bishop81 Peon

    Messages:
    757
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #12
    What does it look like now? We might need to pull the variables out of the string...
     
    Bishop81, Jul 14, 2007 IP
  13. donthate

    donthate Banned

    Messages:
    922
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Exactly:

    $result = mysql_query("SELECT * FROM friends WHERE user='$uid' AND myfriend='$mem');
    
    if ($row = mysql_fetch_array($result)) {
    echo "$mem is already your friend";
    } else {
    mysql_query("INSERT INTO friends (user, myfriend) VALUES('$uid', '$mem')")
    or die(mysql_error());
    
    echo "<b><center><font size='4'><u>$mem</u> is now your friend!</font></u></center></b><br>";
    }
    
    Code (markup):
     
    donthate, Jul 14, 2007 IP
  14. DnHype

    DnHype Active Member

    Messages:
    1,010
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    80
    #14
    ok if you need help pm me i cant know where you are at now so simply pm me if you still need help.
     
    DnHype, Jul 14, 2007 IP
  15. Bishop81

    Bishop81 Peon

    Messages:
    757
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #15
    There was something that I had missed earlier, that might be causing it. Try this first:

    $result = mysql_query("SELECT * FROM friends WHERE user='$uid' AND myfriend='$mem'");

    Add the double quote to the end of the line just before the closing parenthesis.
     
    Bishop81, Jul 14, 2007 IP
  16. donthate

    donthate Banned

    Messages:
    922
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #16
    I think its working! No error codes, and i added someone who is already my friend and it didnt do so, and showed the error :) let me test it for like two more minutes, i already have your paypal but ill let you know anyway!
     
    donthate, Jul 14, 2007 IP
  17. Bishop81

    Bishop81 Peon

    Messages:
    757
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #17
    Good deal. Sorry for missing that earlier, I guess I was just typing too quick so I could be the first to reply. :D
     
    Bishop81, Jul 14, 2007 IP
  18. DnHype

    DnHype Active Member

    Messages:
    1,010
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    80
    #18
    $query = "INSERT INTO friends(id, user, myfriend) VALUES('', '$uid', '$mem' ) ";
    if(!$query)
    {
    echo "error";
    }
    else
    {
    echo "<b><center><font size='4'><u>$mem</u> is now your friend!</font></u></center></b><br>";

    echo "<br><br><center><b><font size='5'><a href='member.php?msg=added'>Back to Member Area</a></font></b></center><br><meta http-equiv='refresh' content='3;url=http://www.**********/member.php?msg=added'>";
    }
     
    DnHype, Jul 14, 2007 IP
  19. donthate

    donthate Banned

    Messages:
    922
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #19
    Sending payment now, thread "closed."
     
    donthate, Jul 14, 2007 IP