Stephen
Stephen

Reputation: 537

PHP: Send ID to HTML button to view Database Row

I have a page that with a HTML table that displays data saved to a PHP database with MySQLi. What Each Event has a unique ID which is generated (auto incrementally) when a new event is added to the DB. On each row there is a View Button, when when clicked, will open up a new page, displaying details about that particular Event only:

$result = mysqli_query($con,"SELECT * FROM events");

    echo "<table border='1'>
    <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Description</th>
    </tr>";

    while($row = mysqli_fetch_array($result))
    {
        echo "<tr>";
        echo "<td>" . $row['id'] . "</td>";
        echo "<td>" . $row['name'] . "</td>";
        echo "<td>" . $row['description'] . "</td>";
        echo "<td><a href='#'' class='btn btn-warning btn-sm'>View</a></td>";
        echo "</tr>";
    }
    echo "</table>";

The problem is, i'm not sure how to send the ID of a particular row in the DB to the button so that it tells the View page to display the details of the Event that is clicked only.

Upvotes: 1

Views: 2927

Answers (2)

Stephen
Stephen

Reputation: 537

Thank you.

It works perfect. In details.php, it is pretty much the same code as events.php. Only difference is:

$result = mysqli_query($con,"SELECT * FROM events WHERE id = '".$_GET['id']."'");

Upvotes: -1

Aaron Averett
Aaron Averett

Reputation: 921

One way to do it might be like this.

echo "<td><a href='#'' onclick=\"requestDetailsPage(".$row['id'].");\" class='btn btn-warning btn-sm'>View</a></td>";

You'd then implement a javascript function that does whatever action you need to request the details from the server, whether that's redirecting to details.php?id=[your id] or doing some sort of ajax operation, etc.

Edit: A simpler solution might be to simply not use a button at all. Like this:

echo "<td><a href='details.php?id=".$row['id']."' target=\"_blank\"  class='btn btn-warning btn-sm'>View</a></td>";

Upvotes: 3

Related Questions