How can the PHP code be optimized to ensure that only the values of the selected user are displayed in the text fields?
To ensure that only the values of the selected user are displayed in the text fields, you can retrieve the user's data from the database based on their ID and populate the text fields with that data. This can be done by adding a WHERE clause to the SQL query to filter results by the selected user's ID.
<?php
// Assuming $selectedUserId contains the ID of the selected user
// Connect to database
$connection = mysqli_connect('localhost', 'username', 'password', 'database');
// Check connection
if (!$connection) {
die("Connection failed: " . mysqli_connect_error());
}
// Retrieve user data based on selected user ID
$sql = "SELECT * FROM users WHERE id = $selectedUserId";
$result = mysqli_query($connection, $sql);
// Populate text fields with user data
if (mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_assoc($result);
$name = $row['name'];
$email = $row['email'];
$phone = $row['phone'];
} else {
echo "User not found";
}
// Close connection
mysqli_close($connection);
?>
<!-- Display user data in text fields -->
<input type="text" name="name" value="<?php echo $name; ?>">
<input type="text" name="email" value="<?php echo $email; ?>">
<input type="text" name="phone" value="<?php echo $phone; ?>">