我有两个表usersuserdata,并使用以下命令创建了它们:

CREATE TABLE users
(
  id INT(6) AUTO_INCREMENT NOT NULL PRIMARY KEY,
  name VARCHAR(255),
  password VARCHAR(255)
);


userdata通过此:

CREATE TABLE userdata
 (
  adder INT(6) NOT NULL,
  added INT(6) NOT NULL,
  CONSTRAINT adder_user FOREIGN KEY (adder) REFERENCES users(id),
  CONSTRAINT added_user FOREIGN KEY (added) REFERENCES users(id),
  PRIMARY KEY (adder, added)
 );


我有一种登录系统,请参见此index.php页:(通过输入其ID和密码为用户添加用户,并为每个用户设置会话)

<?php
  session_start();
?>

    <!DOCTYPE html>
    <html>
    <head>
        <title>
            login
        </title>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
    </head>
    <body>
        <form action="review.php" method="post">
            <input type="text" name="id" placeholder="id">
            <input type="password" name="password" placeholder="password">
        <input type="submit" name="submit" class="btn btn-success">
    </form>
    </body>
    </html>


我已经在review.php中检查了用户的真实性(基本水平)

  <?php

   session_start();
   $connect = mysqli_connect('localhost','root','root','stackoverflow');
if(isset($_POST['submit']))
 {
    $id = $_POST['id'];
    $password = $_POST['password'];


    $query = "SELECT `name`,`id`,`password` from `users` where `id` = '$id' and `password` = '$password' ";
    $run = mysqli_query($connect,$query);
    $row = mysqli_fetch_assoc($run);

    if( ($id == $row['id']) && ($password == $row['password']) )
    {
        // start session with the id of the one who is logged in
        // this is the id of the user who is logged in
        $_SESSION['id']= $row['id'];
        header('location:save.php?id=$id');
    }


}


?>


这是save.php,这是主要问题,假设爱丽丝已登录到我的系统,并且她访问save.php页面将Bob添加到我的系统。
当我的系统(Alice)中的某些登录用户尝试将另一个用户(Bob)添加到我的系统(userdata表)中时,此页面上有保存按钮,此时我要检查此->
    Bob是否已添加Alice(Bob登录时)?如果是这样,当爱丽丝单击保存时,应将名称Bob插入到userdata表中,并且AliceBob's名称应以红色显示,并显示给用户(使用选择查询),因为它们都彼此添加。
如果Alice要添加Bob,但Bob之前未添加Alice,则在按Alice单击“保存”后,应将名称Bob插入userdata表中,并显示为任何颜色,因为Bob尚未添加她。

我该如何解决以下问题:用户添加名称时,必须检查是否将其添加到反向名称中,并在这种情况下进行指示。例如,如果爱丽丝(Alice)添加了鲍勃(Bob),而鲍勃(Bob)已添加了爱丽丝(Alice),您必须指出(带有一些符号)吗?

 <?php
    session_start();

$connect = mysqli_connect('localhost','root','root','stackoverflow');
if(isset($_POST['save']))
{
    $name = $_POST['name'];
    $id   = $_SESSION['id'];
    $insert = "INSERT INTO `userdata` ('adder','added') values ('$id','$name')";

    $query = "SELECT aer.name AS Adder, 'added', aed.name AS added
              FROM users aer
              INNER JOIN userdata ud ON aer.id = ud.adder
              INNER JOIN users aed ON aed.id = ud.added
              WHERE
            (
             SELECT COUNT(*)
                    FROM userdata uda
                    WHERE uda.adder = ud.added
                    AND uda.added = ud.adder
             ) >= 1";

    $run = mysqli_query($connect, $query);
    while($row = mysqli_fetch_assoc($run))
   {
echo "<div class = 'text text-center' id = 'special'>".$row['Adder']."  Added  ".$row['added']."</div><br>";


  }

 }

 ?>
 <!DOCTYPE html>
 <html>
  <head>
   <title>save</title>
   <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
   <link rel="stylesheet" href="style.css">
</head><body>

  <div class="container">
        <div class="row">
            <div class="col-md-6">
                <form action="save.php" method="POST">

                        <input type="text" name="name">
                        <input type="submit" name="save" value="Save" class="btn btn-success">



                </form>
            </div>
        </div>

 </div>

  </body>
 </html>


users

php - 如何在以下系统中检查反向关系-LMLPHP

这是包含加法器和已添加列的userdata

adder    added
1         2
1         3


第一行Alice添加了Bob
第二行Alice添加了John
php - 如何在以下系统中检查反向关系-LMLPHP

最佳答案

首先,您必须具有唯一的用户标识符,以了解是否添加了他(例如,电子邮件)

然后(例如,不要在实际项目中使用)

save.php:

"SELECT `id` from `users` where `email` = '$email'";


如果存在用户,请执行此操作(由于现有用户可以创建用户,因此您已经具有其ID(例如,在$_SESSION['id']中)):

"SELECT * from `userdata` where `edder` = '$id' AND `edded` = '$_SESSION['id']'";


如果记录存在,则意味着将它们添加到相反的位置。

07-25 23:49