我有两个表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
页:(通过输入其用户名和密码来添加用户,并为每个用户设置会话)
<?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
答案 0 :(得分:0)
首先,您必须具有唯一的用户标识符,以便了解是否添加了他(例如电子邮件)
然后(例如,不要在实际项目中使用)
save.php:
"SELECT `id` from `users` where `email` = '$email'";
如果存在用户,请执行此操作(由于现有用户可以创建用户,因此您已经具有其ID(例如,在$_SESSION['id']
中):
"SELECT * from `userdata` where `edder` = '$id' AND `edded` = '$_SESSION['id']'";
如果记录存在,则意味着将它们添加到相反的位置。