我有两个表users
和userdata
,并使用以下命令创建了它们:
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
表中,并且Alice
和Bob'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
表这是包含加法器和已添加列的
userdata
表adder added
1 2
1 3
第一行
Alice
添加了Bob
第二行
Alice
添加了John
最佳答案
首先,您必须具有唯一的用户标识符,以了解是否添加了他(例如,电子邮件)
然后(例如,不要在实际项目中使用)
save.php:
"SELECT `id` from `users` where `email` = '$email'";
如果存在用户,请执行此操作(由于现有用户可以创建用户,因此您已经具有其ID(例如,在
$_SESSION['id']
中)):"SELECT * from `userdata` where `edder` = '$id' AND `edded` = '$_SESSION['id']'";
如果记录存在,则意味着将它们添加到相反的位置。