In PHP, what are some common techniques for retrieving and displaying user data from a database in a form for editing purposes?

When retrieving and displaying user data from a database in a form for editing purposes, you can use SQL queries to fetch the data from the database based on the user's ID. Then, populate the form fields with the retrieved data so the user can edit it. Finally, when the form is submitted, update the database with the edited data.

<?php
// Connect to database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

$conn = new mysqli($servername, $username, $password, $dbname);

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

// Retrieve user data based on ID
$user_id = $_GET['user_id'];
$sql = "SELECT * FROM users WHERE id = $user_id";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // Output data of each row
    $row = $result->fetch_assoc();
    // Display form with populated fields
    echo '<form action="update_user.php" method="post">';
    echo 'Name: <input type="text" name="name" value="' . $row["name"] . '"><br>';
    echo 'Email: <input type="email" name="email" value="' . $row["email"] . '"><br>';
    // Add more fields as needed
    echo '<input type="hidden" name="user_id" value="' . $user_id . '">';
    echo '<input type="submit" value="Update">';
    echo '</form>';
} else {
    echo "User not found";
}

$conn->close();
?>