Home > Backend Development > PHP Tutorial > How do I fetch data from a database to form in PHP?

How do I fetch data from a database to form in PHP?

PHP中文网
Release: 2024-11-21 16:12:31
forward
478 people have browsed it

To fetch data from a database in PHP and display it in a form, you'll typically follow these steps:

  • Connect to the Database: Establish a connection to your database using MySQLi or PDO.

  • Query the Database: Execute a SQL query to retrieve the desired data.

  • Fetch the Data: Retrieve the data from the query result.

  • Populate the Form: Use the fetched data to fill in the form fields.

Here’s a simple example using MySQLi:

Step 1: Connect to the Database

<?php 
$servername = "localhost"; 
$username = "username"; 
$password = "password"; 
$dbname = "database_name";  
// Create connection 
$conn = new mysqli($servername, $username, $password, $dbname); 

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

Step 2: Query the Database

<?php 
$sql = "SELECT id, name, email FROM users WHERE id = 1"; 
// Example query 
$result = $conn->query($sql); 
?>
Copy after login

Step 3: Fetch the Data

<?php 
$user = null; 
if ($result->num_rows > 0) {     
// Fetch associative array     
$user = $result->fetch_assoc(); 
} else {     
echo "No results found."; 
} 
?>
Copy after login

Step 4: Populate the Form

<?php if ($user): ?> 
<form action="update.php" method="post">     
<input type="hidden" name="id" value="<?php echo $user[&#39;id&#39;]; ?>">     
<label for="name">Name:</label>     
<input type="text" id="name" name="name" value="<?php echo htmlspecialchars($user[&#39;name&#39;]); ?>">  
<label for="email">Email:</label>     
<input type="email" id="email" name="email" value="<?php echo htmlspecialchars($user[&#39;email&#39;]); ?>">          
<input type="submit" value="Update"> 
</form> 
<?php endif; ?>
Copy after login

Step 5: Close the Connection

<?php 
$conn->close(); 
?>
Copy after login

Explanation:

  • Database Connection: Replace localhost, username, password, and database_name with your actual database credentials.
  • SQL Query: Adjust the SQL query to fetch the data you need (e.g., by changing the WHERE clause).
  • HTML Form: The form fields are populated with the fetched data. Use htmlspecialchars() to prevent XSS attacks when displaying user input.
  • Form Submission: The form submits to update.php, where you would handle the form data for updating the database.

This example gives you a basic structure to fetch and display data in a form using PHP. Adjust the SQL query and form fields as necessary for your application.

The above is the detailed content of How do I fetch data from a database to form in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template