I've two tables users
and userdata
and I've created them using :
CREATE TABLE users
(
id INT(6) AUTO_INCREMENT NOT NULL PRIMARY KEY,
name VARCHAR(255),
password VARCHAR(255)
);
and userdata
through this:
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)
);
I've kind of login system see this index.php
page:(Here user is added by entering his id an password and session is being set for each user)
<?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>
I've review.php
where I've checked for the user authenticity(on a basic level)
<?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');
}
}
?>
This is save.php
, here is the main issue, Let say Alice is logged into my system, and she visits the save.php
page to add Bob
to my system.
There is save button on this page when some logged in user in my system(Alice
) try to add another user(Bob
) into my system (userdata
table), at that point I want to check this->
has Bob
has already added Alice
(when Bob was logged In)? if so, when Alice clicks on save, the name Bob
should be inserted into userdata
table and Alice
and Bob's
name should be written in red, displayed to the user(using select query), because they both are added by each other.
If Alice
wants to add Bob
but Bob
has not added Alice
before, then after clicking save by Alice
the name Bob
should be inserted into the userdata
table, and should be displayed without any color because Bob
has not yet added her.
How can I solve this problem of When a user adds a name, you must check if they are added to the reverse and in that case to indicate it. For example, if Alice adds Bob and Bob has Added Alice you must indicate it (with some symbol)?
<?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
table
here is userdata
table containing adder and added columns
adder added
1 2
1 3