I am creating an admin dashboard where someone can create user accounts and then edit and delete credentials. I created an edit button next to each user in a table which when clicked will show their id but the button works properly for the second and third user but not the first one, even tho its the same code in doing loop.
I tried deleting the first entry using PHPMyAdmin but then the previous second user who now first has the same issue. I went through the code again to make sure everything is the same and its not happening because I forgot to close off a table or so.
<div class="card-body">
<?php
if(isset($_SESSION['success']) && $_SESSION['success'] !='')
{
echo '<h2>'.$_SESSION['success'].'</h2>';
unset($_SESSION['success']);
}
?>
<?php
include ('includes/dbh.inc.php');
$query = "SELECT * FROM users";
$query_run = mysqli_query($conn,$query)
?>
<table class="table table-striped">
<thead>
<tr>
<th scope="col">User ID</th>
<th scope="col">User Name</th>
<th scope="col">Email</th>
<th scope="col">EDIT</th>
<th scope="col">DELETE</th>
</tr>
</thead>
<tbody>
<?php
if(mysqli_num_rows($query_run) > 0)
{
while($row = mysqli_fetch_assoc($query_run))
{
?>
<tr>
<td><?php echo $row['idUsers'];?></td>
<td><?php echo $row['uidUsers'];?></td>
<td><?php echo $row['emailUsers'];?></td>
<td>
<form action="includes/signup.inc.php" method="post">
<input type="hidden" name="edit_id" value="<?php echo $row['idUsers'];?>">
<button type="submit" name="edit_btn" class="btn btn-success">EDIT</button>
</form>
</td>
<td>
<button type="submit" class="btn btn-danger">DELETE</button>
</td>
</tr>
<?php
}
}
else {
echo "No Record Found";
}
?>
</tbody>
</table>
</div>
$servername = "localhost";
$dBUsername = "root";
$dBPassword = "";
$dBName = "loginsystem";
$conn = mysqli_connect($servername, $dBUsername, $dBPassword, $dBName);
if (!$conn) {
die("Connection failed: ".mysqli_connect_error());
}
if(isset($_POST['edit_btn']))
{
$id = $_POST['edit_id'];
echo $id;
}
I would like to know the mistake I made and if there is a better way to implement this