1

In my project, I have a table and I have fetched data to that table using a while loop. Here's is the code for that.

            <form action="returnDoc.php" method="post">
            <div class="row">
                <div class="col-md-2">

                </div>
                <div class="col-md-8">
                    <div class="row">
                        <div class="col-md-12">
                            <h3>Return Document</h3>
                        </div>
                    </div><br><br>
                    <table class="table table-responsive-lg table-hover">
                        <tr>
                            <th>
                                No
                            </th>
                            <th>
                                Document ID
                            </th>
                            <th>
                                Put the tick for returns
                            </th>
                        </tr>
                        <?php while ($_r = mysqli_fetch_assoc($q_set)) { ?>
                            <tr>
                                <td>
                                    <?php echo $_r['number']; ?>
                                </td>
                                <td>
                                    <?php echo $_r['doc_id']; ?>
                                </td>
                                <td>
                                    <input type="checkbox" name="return" value="<?php echo $_r['doc_id']; ?>" />
                                </td>
                            </tr>
                        <?php } ?>
                    </table>
                </div>
                <div class="col-md-2">

                </div>
            </div>
            <div class="row">
                <div class="col-md-4">

                </div>
                <div class="col-md-4">

                </div>
                <div class="col-md-4">
                    <input type="submit" value="SEND TO DSO" name="submit" class="btn btn-light"/>
                </div>
            </div>
        </form>

As you can see, the table is inside a form and there's a checkbox for each row in the table.

enter image description here

This is a screenshot of my database table. As you can see there's a field called availability and values for them are 'locked'. When I click submit, I want to update these locked fields into 'returned' where checkbox is ticked. Others which are not ticked should not be updated.

So, here's what I did.

    if (isset($_POST['submit'])) {
        $update_query = "UPDATE req SET availability = 'returned' WHERE doc_id='$_POST[return]'";
        mysqli_query($conn, $update_query);
    }

My problem is, this query updates only the last row I ticked because it gets the value after loop is finished. It doesn't update all the rows I select. So if you have any idea how to achieve this, please help me.

Buwaneka Sudheera
  • 1,277
  • 4
  • 17
  • 35

2 Answers2

0
   if (isset($_POST['submit'])) {
    $update_query = "UPDATE req SET availability = 'returned' WHERE doc_id='{$_POST[return]}'";
    mysqli_query($conn, $update_query);
}

try this

for more information please visit When to wrap curly braces around a variable

also, the checkbox will post an array instead of a single variable.

$_POST[return][0]
Wils
  • 1,178
  • 8
  • 24
0

First you need to make sure you are sending an array of ids of the rows that have the checkbox checked.

Then do this in your code:

if (isset($_POST['submit'])) {
    $update_query = "UPDATE req SET availability = 'returned' WHERE doc_id IN " . implode(',', $_POST[return]);
    mysqli_query($conn, $update_query);
}

implode is going to return a string of the array elements glued with the first parameter (',' in our case).

Ali Elkhateeb
  • 3,413
  • 4
  • 21
  • 39