Preview:
<?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);
}

// Check if ID is provided in the URL
if (!isset($_GET['id'])) {
    echo "No ID provided";
    exit();
}

$id = $_GET['id'];

// Fetch the record to be edited based on the ID
$sql_select = "SELECT id, username, email, age FROM users WHERE id = $id";
$result = $conn->query($sql_select);

if ($result->num_rows > 0) {
    $row = $result->fetch_assoc();
} else {
    echo "No record found";
    exit();
}

// Update record if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $new_username = $_POST['username'];
    $new_email = $_POST['email'];
    $new_age = $_POST['age'];

    $sql_update = "UPDATE users SET username = '$new_username', email = '$new_email', age = '$new_age' WHERE id = $id";

    if ($conn->query($sql_update) === TRUE) {
        // Updated successfully, you can redirect or show a message
        header("Location: your_page.php"); // Redirect to your page
        exit();
    } else {
        echo "Error updating record: " . $conn->error;
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Edit User</title>
    <style>
        /* Your CSS styles */
    </style>
</head>
<body>

<h2>Edit User</h2>

<form method="post">
    <label for="username">Username:</label><br>
    <input type="text" id="username" name="username" value="<?php echo $row['username']; ?>"><br>

    <label for="email">Email:</label><br>
    <input type="text" id="email" name="email" value="<?php echo $row['email']; ?>"><br>

    <label for="age">Age:</label><br>
    <input type="number" id="age" name="age" value="<?php echo $row['age']; ?>"><br><br>

    <input type="submit" value="Submit">
</form>

</body>
</html>

<?php
// Close connection
$conn->close();
?>
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter