Ad

Php Only Updating First Database Entry Regardless Of What Entry I Try To Update

- 1 answer

So I'm doing a project for school, and I am trying to update certain items in column for certain rows in my database, however, regardless of what entry I try to update, only the first entry takes on the intended change.

Here is my update function:

if (isset($_POST['update']) && isset($_POST['serviceNum']))
{
    $progress = get_post($conn, 'progress');
    $serviceNum = get_post($conn, 'serviceNum');
    $query = "UPDATE classics SET progress='$progress' WHERE serviceNum='$serviceNum'";
    $result = $conn->query($query);
    if (!$result) echo "UPDATE failed: $query<br>" .
        $conn->error . "<br><br>";
}

And here is my update form:

<form action="admin.php" method="post">
                    <br>

   <select name="progress">
        <option value=" "> </option>
        <option value="In Progress">In Progress</option>
        <option value="Completed">Completed</option>
    </select>

    <input type="hidden" name="update" value="progress">
    <input type="hidden" name="serviceNum" value="$row[0]">

    <br><br>
    <input type="submit" value="UPDATE">
    <br><br>
</form>

$row[0] is the row that holds the primary key which is serviceNum. I have the form nested in a table, and when I run it on my localhost, and view source, it shows that each entry has the proper serviceNum for that row of data. However, regardless of which entry I use the update button on, it only updates the first entry of the database table.

EDIT:

for ($j = 0; $j < $rows; ++$j)
{
    $result->data_seek($j);
    $row = $result->fetch_array(MYSQLI_NUM);

    echo "<tr>";

    echo <<<_END
    <td>$row[0]</td>
    <td>$row[1]</td>
    <td>$row[2]</td>
    <td>$row[3]</td>
    <td>$row[4]</td>
    <td>

        <!-- Trigger Update Modal -->
        <center>
        <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#UpdateModal"> <span class="glyphicon glyphicon-folder-open" aria-hidden="true"></span> </button>
        </center>

        <!-- Update Modal -->

        <div class="modal fade" id="UpdateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
            <div class="modal-dialog"role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                        <h4 class="modal-title" id="myModalLabel">Update Record</h4>
                    </div>
                    <div class="modal-boday">
                        <center>
                        <form action="admin.php" method="post">
                            <br>

                            <select name="progress">
                                <option value=" "> </option>
                                <option value="In Progress">In Progress</option>
                                <option value="Completed">Completed</option>
                            </select>

                            <input type="hidden" name="update" value="yes">
                            <input type="hidden" name="serviceNum" value="$row[0]">

                            <br><br>
                            <input type="submit" value="UPDATE">

EDIT 2: MODAL

                            <center>
                                <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#UpdateModal">
                                    <span class="glyphicon glyphicon-folder-open" aria-hidden="true"></span>
                                </button>
                            </center>

                            <!-- Update Modal -->

                            <div class="modal fade" id="UpdateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
                                <div class="modal-dialog" role="document">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                <span aria-hidden="true">&times;</span>
                                            </button>
                                            <h4 class="modal-title" id="myModalLabel">Update Record</h4>
                                        </div>
                                        <div class="modal-boday">
                                        <center>
                                            <form action="admin.php" method="post">
                                                <br>
                                                <select name="progress">
                                                    <option value=" "> </option>
                                                    <option value="In Progress">In Progress</option>
                                                    <option value="Completed">Completed</option>
                                                </select>

                                                <input type="hidden" name="update" value="status">
                                                <input type="hidden" name="ticketnum" value="$row[0]">
                                                <br><br>
                                                <input type="submit" value="UPDATE">
                                                <br><br>
                                            </form>
                                        </center>
                                    </div>
                                </div>
                            </div>

                            <!-- End Update Modal -->
Ad

Answer

The problem is that you're using the same id="update-modal" in every row of the table. IDs are supposed to be unique, and looking up an ID always returns the first one. So data-target="#update-modal" always targets the modal on the first row, regardless of which one you click.

Add the item ID into the update-modal ID.

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#UpdateModal{$row[0]}">

and

<div class="modal fade" id="UpdateModal{$row[0]}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
Ad
source: stackoverflow.com
Ad