PHP datatable view

PHOTO EMBED

Wed Nov 15 2023 00:14:35 GMT+0000 (Coordinated Universal Time)

Saved by @prasit12

<?php
// Replace with your database credentials
$servername = "your_servername";
$username = "your_username";
$password = "your_password";
$dbname = "your_database";

// Create connection using MySQLi
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Delete record if delete button is clicked
if (isset($_GET['delete_id'])) {
    $delete_id = $_GET['delete_id'];
    $sql_delete = "DELETE FROM users WHERE id = $delete_id";
    if ($conn->query($sql_delete) === TRUE) {
        // Deleted successfully, you can redirect or show a message
        header("Location: your_page.php"); // Redirect to your page
        exit();
    } else {
        echo "Error deleting record: " . $conn->error;
    }
}

// SQL query to select data from the users table
$sql = "SELECT id, username, email, age FROM users";
$result = $conn->query($sql);
?>

<!DOCTYPE html>
<html>
<head>
    <title>User Data Table</title>
    <style>
        /* Your CSS styles for the table */
    </style>
</head>
<body>

<h2>User Data Table</h2>

<table>
    <thead>
        <tr>
            <th>ID</th>
            <th>Username</th>
            <th>Email</th>
            <th>Age</th>
            <th>Edit</th>
            <th>Delete</th>
        </tr>
    </thead>
    <tbody>
        <?php
        if ($result->num_rows > 0) {
            while ($row = $result->fetch_assoc()) {
                echo "<tr>";
                echo "<td>" . $row["id"] . "</td>";
                echo "<td>" . $row["username"] . "</td>";
                echo "<td>" . $row["email"] . "</td>";
                echo "<td>" . $row["age"] . "</td>";
                echo "<td><a href='edit.php?id=" . $row["id"] . "'>Edit</a></td>"; // Link to edit.php with ID
                echo "<td><a href='?delete_id=" . $row["id"] . "' onclick='return confirm(\"Are you sure?\")'>Delete</a></td>"; // Delete link with confirmation
                echo "</tr>";
            }
        } else {
            echo "<tr><td colspan='6'>No data found</td></tr>";
        }
        ?>
    </tbody>
</table>

</body>
</html>

<?php
// Close connection
$conn->close();
?>
content_copyCOPY

https://chat.openai.com/c/acf7d762-ee33-46a0-9b00-c2b296d98cfa