2

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.

1
  • This happens because the values of $ID and $Email will 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. Try WHERE IN. Commented Apr 26, 2015 at 15:24

3 Answers 3

3

First, you'll need to move the closing loop brace to the end of the html of each row. Then I recomend you to do a separated URL for deleting a record, and changing the submit button to a anchor tag (by CSS rules you can make it to look like a button), so the anchor will be:

<a href="delete.php?id=<?php echo $ID?>" class="button" onclick="return confirm('Are you sure you want to completely remove this user?');">Delete</a>

So in the backend of delete.php you'll have something like:

if(isset($_GET['id'])){
    $dbQuery = $db->prepare(sprintf("DELETE FROM user WHERE ID = %s", $_GET['id']));
    $dbQuery->execute(array($Name));
}

You'll need to improve this delete query so it will be safe, because people might send you SQL code trough $_GET['id']

Sign up to request clarification or add additional context in comments.

Comments

1

Your while loop is wrong, because last one is fetched and then html is rendered. Try add that html under while loop

Comments

0

Please add ID in hidden filed in a form. So you can get id each form submit like normal form field value. Add ID to hidden field value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.