Mitch
Mitch

Reputation: 1173

Table Loop Updates only last value

I have a page which is divided in 2 tables. 1 is for people who are active. 1 is for people who are inactive.

The active and inactive part is based on the values which are in a database. The table ooks like:

gebruiker_id    naam    aanwezig
----------------------------------
1               Mitch       0
2               Mitch2      1

If aanwezig is set to 1, the name will appear in the left table. If the value aanwezig is set to 0, the name will appear in the right table.

I am displaying the tables with the data which looks like:

enter image description here

Behind every name I have created a button which calls a function to update the value aanwezig to either 0 or 1. This works but always seems to update the latest userID and not the one I clicked. If for example I press the button behind Mitch1 (UserID 1), Mitch2 (UserID2) will be updated. It seems to be an issue with the loop but I can't seem to figure it out.

I have pasted my code which is placing the users from Afwezig to Aanwezig (change it from 0 to 1)

<form method="POST">
    <table class="table table-striped">
        <thead>
            <tr>
                <th class="col-md-1">Naam</th>
                <th class="col-md-2">Inchecken</th>
            </tr>
        </thead>
        <tbody>
        <!--Loop door alle afwezigen in het pand-->
         <?php
             foreach($afwezigheid as $afwezigheid){
                 $naam_afwezig = $afwezigheid['naam'];
                 $gebruiker_id = $afwezigheid['gebruiker_id'];
         ?>
             <tr>
                 <td class="col-md-1"><?= $naam_afwezig ?></td>
                 <input type="hidden" name="aanmelden" value="<?= $gebruiker_id ?>">
                 <td class="col-md-2"><input class="btn btn-primary btn-sm" name="aanmeld_knop" value="<?= $gebruiker_id ?>" type="submit"></td>
             </tr>
             <?php
                 }//end loop
                 if(isset($_POST['aanmeld_knop'])){
                     $gebruikers_id =  $_POST['aanmelden'];
                     $database->aanmelden($gebruikers_id, $aanwezig);
                 }
             ?>
        </tbody>
    </table>
</form>

And the database function:

//functie die de aanwezigheid op 0 zet van een specifieke gebruiker

function aanmelden($gebruikers_id, $aanwezig) {
    $sql = "UPDATE gebruikers SET aanwezig = :aanwezig WHERE gebruiker_id = :gebruiker_id";
    $sth = $this->pdo->prepare($sql);
    $sth->bindParam(':gebruiker_id', $gebruikers_id, PDO::PARAM_INT);
    $sth->bindParam(":aanwezig", $aanwezig, PDO::PARAM_INT);
    $sth->execute();
    header("Refresh:0");
}

The gebruiker_id is used correctly as you can see in the image. But if I want to update user 1, user 2 receives the update.

I would like to know where it went wrong and how I can solve this.

Upvotes: 0

Views: 43

Answers (1)

Swati
Swati

Reputation: 28522

As your form tag is outside table so only last value will get submitted because there multiple input with same name . So , to overcome this one way is to put <form></form> tag around your button i.e :

<td class="col-md-2">
    <form method="POST">
     <!--this will get submit -->
      <input type="hidden" name="aanmelden" value="<?= $gebruiker_id ?>">
      <input class="btn btn-primary btn-sm" name="aanmeld_knop" value="<?= $gebruiker_id ?>" type="submit">
   </form>
</td>

Now, when you will click on submit button only value which is inside form will get submit to server .

Upvotes: 1

Related Questions