I have a page that list all of the users from mysql database. I put a delete button in it so you can delete the profile instantly. But I didn't want to delete the user instantly so I put a code that create a modal box when you click onto the delete button. My problem is when I click onto the first user's delete button, or any other users button its only delete the last user's data and I don't know why. Without the modal box the delete function works fine. Any idea whats the problem?
Here's the code:
<?php
include "dbConn.php";
session_start();
// If the user is not logged in redirect to the login page...
if (!isset($_SESSION['loggedin'])) {
header('Location: /admin/index.php');
exit;
}
$DATABASE_HOST = 'localhost';
$DATABASE_USER = 'root';
$DATABASE_PASS = '';
$DATABASE_NAME = 'phplogin';
$con = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);
if (mysqli_connect_errno()) {
exit('Failed to connect to MySQL: ' . mysqli_connect_error());
}
$stmt = $con->prepare('SELECT password, email, timestamp FROM accounts WHERE id = ?');
$stmt->bind_param('i', $_SESSION['id']);
$stmt->execute();
$stmt->bind_result($password, $email, $timestamp);
$stmt->fetch();
$stmt->close();
?>
<?php
if ($_SESSION['name'] != 'root') {
echo '<script>';
echo 'window.location.href="/admin/403.php?from=provisions=denied"';
echo '</script>';
}
?>
<?php require_once($_SERVER['DOCUMENT_ROOT'].'/admin/includes/header.php'); ?>
<section class="prfileMenu tabcontent" id="Profiles">
<div class="profile_header">
<span class="page_title">Provisions</span>
<hr class="page_title_hr">
<?php
include "dbConn.php"; // Using database connection file here
$records = mysqli_query($db,"select * from accounts"); // fetch data from database
while($data = mysqli_fetch_array($records)) {}
$conn = mysqli_connect("localhost", "root", "", "phplogin");
if ($conn-> connect_error) {
die("Connection failed:". $conn-> connect_error);
}
$ssql = "SELECT * FROM accounts";
$result = $conn-> query($ssql);
if ($result-> num_rows > 0) {
while ($roww = $result-> fetch_assoc()) {
$id = $roww['id'];
echo "
<div class='profile-container-wrapper'>
<div class='prof-datas-title'>".$roww["username"]."</div>
<div class='profile-datas-container provision-table'>
<table>
<tr>
<td>User ID:</td>
<td>". $roww["id"] ."</td>
<td>
<a class='prov-delete-prof' id='btn".$roww['id']."' onclick='pop();' title='Delete ".$roww['username']."'>Delete</a>
<a class='edit-data' href='edit-datas.php?id=".$roww['id']."'>Edit</a>
</td>
</tr>
<tr>
<td>Username:</td>
<td>". $roww["username"] ."</td>
<td></td>
</tr>
<tr>
<td>Real Name:</td>
<td>". $roww["realname"] ."</td>
<td></td>
</tr>
<tr>
<td>Email:</td>
<td>". $roww["email"] ."</td>
<td></td>
</tr>
<tr>
<td>Second Email:</td>
<td>". $roww["second_email"] ."</td>
<td></td>
</tr>
<tr>
<td>Password:</td>
<td id='provision-pass'>". $roww["password"] ."</td>
<td></td>
</tr>
<tr>
<td>Member since:</td>
<td>". $roww["timestamp"] ."</td>
<td></td>
</tr>
<tr>
<td>Sex:</td>
<td>". $roww["sex"] ."</td>
<td></td>
</tr>
<tr>
<td>Age:</td>
<td>". $roww["age"] ."</td>
<td></td>
</tr>
<tr>
<td>Country:</td>
<td>". $roww["country"] ."</td>
<td></td>
</tr>
<tr>
<td>City:</td>
<td>". $roww["city"] ."</td>
<td></td>
</tr>
<tr>
<td>2FA:</td>
<td>Disabled</td>
<td></td>
</tr>
<tr>
<td>Activation code:</td>
<td>". $roww["activation_code"] ."</td>
<td></td>
</tr>
</table>
</div>
</div>
<div class='delete-modal' id='box".$roww['id']."'>
<img src='/assets/images/svg/rf-alert.svg' width='128px'>
<h1>Attention!</h1>
<p>You are going to delete this user permanently.</p>
<a class='close' id='dlt".$roww['id']."' href='delete.php?id=".$roww['id']."' title='".$roww['id']."'>Delete</a>
<a class='close' onclick='pop()'>Cancel</a>
</div>
<script>
var modal = null
function pop() {
if(modal === null) {
document.getElementById('box".$roww['id']."').style.display = 'block';
modal = true
} else {
document.getElementById('box".$roww['id']."').style.display = 'none';
modal = null
}
}
</script>
";
}
} else {
echo "0 result";
}
$conn-> close();
?>
</section>
</main>
<script src="/admin/includes/assets/js/status.js"></script>
<script>
var coll = document.getElementsByClassName("collapsible");
var i;
for (i = 0; i < coll.length; i++) {
coll[i].addEventListener("click", function() {
this.classList.toggle("active");
var content = this.nextElementSibling;
if (content.style.maxHeight){
content.style.maxHeight = null;
} else {
content.style.maxHeight = content.scrollHeight + "px";
}
});
}
</script>
<?php require_once($_SERVER['DOCUMENT_ROOT'].'/admin/includes/footer.php'); ?>