Omid Reza Abbasi Omid Reza Abbasi - 4 months ago 19
MySQL Question

How to query from a HTML table filled with database rows in PHP and MySQL?

I have an HTML table in which rows are filled with information stored in a database. In one of the columns of the table, there are buttons. What I want to do is that whenever these buttons are clicked, the corresponding values in the database table should change to 'True'. But, the problem is that I don't know how to query in a way that the right row be selected. Here is my table:

Here is the table image


It sounds like you're just not sure how to identify which row you clicked on. There are a few different ways to do that. Here's a brief example of one way:

In your HTML document, wrap your table in a form, and use a unique identifier of each row as the value of a submit button.

<form method="post" action="form_handler.php">
        <?php foreach ($query_result as $row): ?>
                <!-- The value of the button represents the id of the clicked row -->
                <button name="id" value="<?= $row->id ?>" type="submit">True</button>
            <td><?= $row->other_column ?></td>
            <td><?= $row->more_columns_etc ?></td>
        <?php endforeach ?>

When you click the button, the form will be submitted. Then in your PHP code that handles the form submission, the id will be available in $_POST['id'], and you can use it in the WHERE clause of your UPDATE query.

// form_handler.php
$sql = "UPDATE your_table SET your_boolean_column=1 WHERE id=?"