There is a table of friends if a friend added a friend is displayed as add_fri = 0, friend = 1 means that the user with id 0 added a friend with id 1

How to make the addition check if there is a list, then wrote "The player is already your friend"

$friend = $_GET['add_fri']; $f = mysql_fetch_array(mysql_query("SELECT * FROM `db_users` WHERE steam_id = '$friend' ")); $add = $_SESSION['steam_id']; $frien = mysql_query("SELECT * FROM `friends` WHERE add_fri = '$add'"); while ($friends = mysql_fetch_array($frien)) { if ($friends['friend'] == $$_GET['add_fri']) { echo "<div class='status-error'>Игрок (".$f['username'].") уже есть в друзьях.</div> <script>$('.status-error').slideDown('slow'); setTimeout(function() { $('.status-error').slideUp('slow'); }, 3000); </script>"; } else if (!$friends['friend'] == $_GET['add_fri']) { echo "<div class='status-accept'>Игрок (".$f['username'].") был добавлен в друзья.</div> <script>$('.status-accept').slideDown('slow'); setTimeout(function() { $('.status-accept').slideUp('slow'); }, 3000); </script>"; $fri = mysql_fetch_array(mysql_query("INSERT INTO friends (add_fri,friend) VALUES ('$add','$friend')")); } } 
  • make a unique index on the fields add_fri and friend and catch the error of violation of uniqueness after trying to add a friend. If there are no other limitations of uniqueness (most likely this is the case), then it is immediately clear what the matter is. - Sergey
  • Do you recommend Primary key? - Sauron
  • The fields work like this: add_fri = 0 friend = 1, add_fri = 0 friend = 2, If I make an example, they will not add each other - Sauron
  • May and may. Also unique. SQL error code if I'm not mistaken 23505. Add. Uniqueness or primari in two fields. Unique must be a couple. (0, 1)! = (0, 2) - Sergey
  • If I do this, then one player will not be able to add more than one friend. - Sauron

2 answers 2

PHP completely forgot. But I think the idea is clear. Something like this

 if (mysql_query("INSERT INTO friends (add_fri,friend) VALUES ('$add','$friend')")) { print "Теперь у Вас есть новый друг"; } else { if (mysql_errno() == 1169) { print "Это старый друг"; } else { print "Какая-то непонятная ошибка"; } } 

If there is a restriction of uniqueness on the pair (add_fri, friend). And only for her. Otherwise, you will not understand (it is extremely difficult) for what exactly the error 1169 occurred. And this will be its only reason.
By the way, the error number is desirable to clarify in the manual or the method of scientific typing in the SQL server console.

  • Everything earned sp.Ne knew that inside if you can make a request). - Sauron
  • Yes. Can. It is possible and outside if : $r = mysql_query(...); if ($r) { ... } $r = mysql_query(...); if ($r) { ... } - Sergey

Do not display the add button if the player is already a friend, that's all. Why complicate things? On the server, when adding, also check whether there is already such a record in the database, in case of a high-loaded application, you can use transactions. And yes, the primary key at the base level should be required.

And use PDO instead of mysql_query, or at least on mysqli_query, otherwise there will be problems on the latest versions of the PHP interpreter.

  • This is of course all right about the button. But on the other hand, nothing prevents to open the page in two windows. In one add a friend and when the button disappears, switch to the second window. And in the second window there will again be this button. - Sergey
  • So I write, do a server check for this case, from one SELECT COUNT (*) will not be worse, and if the record has already been added, return the corresponding answer. If the simultaneous recording check is of fundamental importance to you, learn the mechanism of Locker-s, or transactions - Daniel-664
  • We are supporters of optimistic locks. Locker and transaction level serializable only in extreme cases. Adding a friend does not apply to them. On the fig stand in line of locks / transactions, even if a negative result - this is also good. even better. - Sergey
  • I wanted to do it if he is already in friends, then let him display the "-" button, that is, delete from friends. But x3 as the brain froze) ... - Sauron
  • one
    Everything solved the problem made the delete and add button - Sauron