简体   繁体   中英

checkboxes generated dynamically not inserting all values into array

I have a query that selects all of the users in a database that are not approved.

$query = "SELECT * FROM users WHERE approved = '0';
$data = mysqli_query($dbc, $query);

Then, what I want to do is display that list of people in a table that has a column with a checkbox to approve that person.

<table class="table mb-none">
    <thead>
        <tr>
            <th>Picture</th>
            <th>Username</th>
            <th>Profile Type</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Email</th>
            <th>Phone Number</th>
            <th>Approve</th>
            <th>Deny/Delete</th>
        </tr>
    </thead>
    <tbody>

  <?php
   while($row = mysqli_fetch_array($data)){
      echo '<tr>'
            echo '<td><img width="70px" height:"90px" src="' . EHS_UPLOADPATH_STUDENTS . $row['picture'] . '"/></td>';
            echo '<td>' . $row['username'] . '</td>';
            echo '<td>Student</td>';
            echo '<td>' . $row['first_name'] . '</td>';
            echo '<td>' . $row['last_name'] . '</td>';
            echo '<td>' . $row['email'] . '</td>';
            echo '<td>' . $row['phone_number'] . '</td>';
            echo '<td style="text-align:center; padding:20px; background-color:#DFF0D8;">';
            echo '<input type="checkbox" name="approve[]" value = "'.$row['user_id'].'"></td>';
            echo '<td style="text-align:center; padding:20px; background-color:#FCDEDE;"><a href="profile_delete_info.php?user_id='. urlencode($row['user_id']) . '&amp;first_name=' . urlencode($row['first_name']) . '&amp;last_name=' . urlencode($row['last_name']) .'"><button type="button" class="mb-xs mt-xs mr-xs btn btn-danger">Deny/Delete</button></a>  </td>';
       echo '</tr>';}

在此处输入图片说明

This is how I am trying to process this:

if(isset($_POST['submit'])){
    if(!empty($_POST['approve'])){
        $users = $_POST['approve'];
        foreach($users as $user){
            $query = "UPDATE users SET approved = '1' WHERE user_id = '". $user . "'";
            mysqli_query($dbc, $query);}}} ** here goes the rest of the code

Now, the problem I am having is that when I click on the "approve" checkboxes and hit "Submit" only the first item in the array approve[] is being processed and not the rest. I have no idea why. I have been thinking about this for the last 2 hours and can't figure it out.

try the following approach :

if(isset($_POST['submit'])){
if(!empty($_POST['approve'])){
    foreach($_POST['approve'] as $user){
        $query = "UPDATE users SET approved = '1' WHERE user_id = '". $user . "'";
        mysqli_query($dbc, $query);}}} ** here goes the rest of the code

If that does not work ,do a print_r($_POST['approve']) and see what the contents of the array are.

I found the solution... I put the closing bracket for the "foreach" in the wrong place. Thanks for your help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM