I am displaying user details on a form using a for each loop. In the for each loop i am adding a button below the user details to delete the information for each user.
<?php
foreach( $Names as $Name )
{
$dbQuery = $db->prepare("select * from user WHERE Name = ?");
$dbQuery->execute(array($Name));
while ($dbRow = $dbQuery->fetch(PDO::FETCH_ASSOC))
{
$ID = $dbRow['ID'];
$Email = $dbRow ['Email'];
}
?>
<div class="form-group">
<label for="hotel_id" class=" col-sm-4 control-label">ID:</label>
<div class="col-sm-8 input">
<input type="text" class="form-control" id="ID" name="ID[]" placeholder="Enter City Name" value="<?php echo $ID?>">
</div>
</div>
<div class="form-group">
<label for="Name" class="col-sm-4 control-label">Name:</label>
<div class="col-sm-8 input">
<input type="text" class="form-control" id="Name" name="Name[]" value="<?php echo $Name?>">
</div>
</div>
<div class="form-group">
<label for="Email" class="col-sm-4 control-label">Email:</label>
<div class="col-sm-8 input">
<input type="text" class="form-control" id="Email" name="Email[]" value="<?php echo $Email ?>">
</div>
</div>
<div class ="form-group">
<label for="removeUser" class="col-sm-4 control-label"></label>
<div class="col-sm-8">
<input id="removeUser" name="removeUser" type="submit" value="Remove this user" class="btn btn-danger" onclick="return confirm('Are you sure you want to completely remove this user?');">
</div>
</div>
<?php
}
?>
I know how to delete the users but the problem i am having is that i cant seem to get the id for the specific user when the button is clicked. So far i have only been able to retrieve the last id displayed or all of the id's displayed.
Any help would be appreciated. Thanks.
$IDand$Emailwill change for every loop. Therefore, the values that are used in the HTML will be based on the ones fetched in the last db query. Also, it is not efficient to have a query run inside a foreach loop. TryWHERE IN.