Are there any specific PHP functions or techniques that can help streamline the process of populating form fields with database content?
When populating form fields with database content in PHP, you can use functions like mysqli_fetch_assoc() to retrieve data from the database and then assign the values to the form fields. You can streamline this process by creating a reusable function that fetches the data and populates the form fields dynamically.
// Function to populate form fields with database content
function populateFormFields($conn, $table, $id) {
$query = "SELECT * FROM $table WHERE id = $id";
$result = mysqli_query($conn, $query);
$data = mysqli_fetch_assoc($result);
foreach ($data as $key => $value) {
echo "<input type='text' name='$key' value='$value'><br>";
}
}
// Usage example
$conn = mysqli_connect("localhost", "username", "password", "database");
populateFormFields($conn, "users", 1);