Update Data using PHP & MySQL Database
Updating data in a MySQL database is a fundamental operation in web development. This guide will demonstrate how to update records in a MySQL database using core PHP.
Prerequisites
- Basic understanding of PHP and MySQL.
- A MySQL database set up with a table to update records.
- A web server (e.g., XAMPP, WAMP, or a live server) to run PHP scripts.
Steps to Update Data in MySQL
- Create a Database and Table
CREATE DATABASE demo_db;
USE demo_db;
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL,
age INT NOT NULL
);
INSERT INTO users (name, email, age) VALUES
('John Doe', 'john.doe@example.com', 25),
('Jane Smith', 'jane.smith@example.com', 30);
- Establish a Database Connection in PHP
<?php
// Database connection settings
$host = 'localhost';
$user = 'root';
$password = ''; // Use your database password
$database = 'demo_db';
// Create connection
$conn = new mysqli($host, $user, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
- Create a Form to Collect Data
<?php
// Fetch the existing data for the user to edit
$id = $_GET['id'];
$sql = "SELECT * FROM users WHERE id = $id";
$result = $conn->query($sql);
$user = $result->fetch_assoc();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Update User</title>
</head>
<body>
<h2>Update User Information</h2>
<form action="update.php" method="POST">
<input type="hidden" name="id" value="<?php echo $user['id']; ?>">
<label for="name">Name:</label>
<input type="text" id="name" name="name" value="<?php echo $user['name']; ?>" required><br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" value="<?php echo $user['email']; ?>" required><br><br>
<label for="age">Age:</label>
<input type="number" id="age" name="age" value="<?php echo $user['age']; ?>" required><br><br>
<button type="submit">Update</button>
</form>
</body>
</html>
- Handle the Update Request in PHP
Create a file named update.php to process the form submission and update the database.
<?php
// Include the database connection file
include 'db_connection.php';
// Get data from the form
$id = $_POST['id'];
$name = $_POST['name'];
$email = $_POST['email'];
$age = $_POST['age'];
// Prepare the SQL query to update data
$sql = "UPDATE users SET name = ?, email = ?, age = ? WHERE id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ssii", $name, $email, $age, $id);
// Execute the query
if ($stmt->execute()) {
echo "Record updated successfully.";
} else {
echo "Error updating record: " . $conn->error;
}
// Close the connection
$stmt->close();
$conn->close();
?>
Explanation of the Code
- Database Connection: Establishes a connection to the MySQL database.
- Fetching Data: Retrieves the existing data for the record to populate the form.
- Form Submission: Sends the updated data to the
update.phpscript via POST. - Prepared Statement: Prevents SQL injection by using parameterized queries to update the record.
- Execution: Updates the record in the database and provides feedback to the user.
Final Notes
- Ensure proper validation and sanitization of input data to secure your application.
- Use HTTPS for secure data transmission.
- Test thoroughly to handle edge cases, such as invalid inputs or database errors.
Related Blogs
May 14, 2026
33 Views
May 02, 2026
118 Views
Apr 26, 2026
116 Views
Apr 21, 2026
133 Views
Apr 19, 2026
112 Views
Apr 15, 2026
118 Views
Mar 23, 2026
620 Views
Mar 16, 2026
547 Views
Mar 09, 2026
359 Views
Mar 02, 2026
237 Views
Feb 25, 2026
567 Views
Feb 18, 2026
949 Views
Feb 09, 2026
293 Views
Jan 31, 2026
309 Views
Jan 10, 2026
3141 Views
Dec 30, 2025
428 Views
Dec 20, 2025
444 Views
Dec 04, 2024
2191 Views
Nov 29, 2024
1809 Views
Nov 25, 2024
1519 Views
Nov 18, 2024
3106 Views
Nov 13, 2024
2099 Views
Nov 11, 2024
1840 Views
Nov 06, 2024
2144 Views
Nov 04, 2024
1641 Views
Oct 30, 2024
2225 Views
Oct 28, 2024
2183 Views
Oct 23, 2024
3122 Views
Oct 21, 2024
1763 Views
Oct 16, 2024
2483 Views
Oct 14, 2024
2964 Views
Oct 09, 2024
1718 Views
Oct 07, 2024
1892 Views
Oct 04, 2024
2492 Views
Sep 30, 2024
3608 Views
Sep 23, 2024
1597 Views
Sep 20, 2024
2871 Views
Aug 02, 2024
1933 Views
Jun 08, 2023
1467 Views
Jun 01, 2023
1642 Views
May 19, 2023
3155 Views
Jul 05, 2022
4778 Views
May 08, 2022
7608 Views
Apr 17, 2022
28407 Views
Dec 02, 2021
5259 Views
Sep 13, 2021
4524 Views
May 29, 2021
5266 Views
Apr 27, 2021
3525 Views
Apr 06, 2021
5151 Views
Apr 03, 2021
3981 Views
Mar 26, 2021
2637 Views
Mar 19, 2021
3417 Views
Mar 19, 2021
2716 Views
Mar 05, 2021
2751 Views
Feb 18, 2021
3794 Views
Feb 18, 2021
3205 Views
Feb 11, 2021
2759 Views
Nov 10, 2020
3003 Views
Sep 09, 2020
4284 Views
Aug 24, 2020
4675 Views
Aug 20, 2020
4274 Views
Aug 18, 2020
5842 Views
Jul 10, 2020
8131 Views
Jul 07, 2020
11780 Views
Jul 07, 2020
9986 Views
Jul 07, 2020
12603 Views
Jul 07, 2020
8252 Views
Jul 04, 2020
18067 Views
Jul 04, 2020
9603 Views
Jul 04, 2020
6666 Views
Jul 04, 2020
3372 Views
Jul 03, 2020
9751 Views
Jul 01, 2020
8012 Views
Jul 01, 2020
5648 Views
Jun 30, 2020
13481 Views
Jun 30, 2020
10312 Views
Jun 23, 2020
7615 Views
Jun 17, 2020
7831 Views
Jun 16, 2020
5808 Views
Jun 12, 2020
9162 Views
Jun 09, 2020
6979 Views

















.png)
_(1).jpg)
_(2).jpg)
.jpg)

_(1).jpg)

.jpg)
.jpg)
.jpg)
.jpg)
.jpg)


.jpg)
_(1).jpg)
.jpg)
.jpg)


_(1).png)

.png)

1.png)






























4.png)









0 Replies to “Update Data Using PHP & MySql Database”
Leave a Reply
Your email address will not be published.