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 28, 2026
43 Views
May 14, 2026
114 Views
May 02, 2026
169 Views
Apr 26, 2026
192 Views
Apr 21, 2026
155 Views
Apr 19, 2026
128 Views
Apr 15, 2026
141 Views
Mar 23, 2026
841 Views
Mar 16, 2026
619 Views
Mar 09, 2026
396 Views
Mar 02, 2026
266 Views
Feb 25, 2026
637 Views
Feb 18, 2026
1080 Views
Feb 09, 2026
327 Views
Jan 31, 2026
333 Views
Jan 10, 2026
4634 Views
Dec 30, 2025
467 Views
Dec 20, 2025
482 Views
Dec 04, 2024
2222 Views
Nov 29, 2024
1822 Views
Nov 25, 2024
1543 Views
Nov 18, 2024
3177 Views
Nov 13, 2024
2148 Views
Nov 11, 2024
1861 Views
Nov 06, 2024
2177 Views
Nov 04, 2024
1653 Views
Oct 30, 2024
2258 Views
Oct 28, 2024
2232 Views
Oct 23, 2024
3155 Views
Oct 21, 2024
1780 Views
Oct 16, 2024
2520 Views
Oct 14, 2024
3032 Views
Oct 09, 2024
1737 Views
Oct 07, 2024
1909 Views
Oct 04, 2024
2572 Views
Sep 30, 2024
3788 Views
Sep 23, 2024
1648 Views
Sep 20, 2024
2923 Views
Aug 02, 2024
1956 Views
Jun 08, 2023
1477 Views
Jun 01, 2023
1662 Views
May 19, 2023
3235 Views
Jul 05, 2022
4802 Views
May 08, 2022
7694 Views
Apr 17, 2022
28499 Views
Dec 02, 2021
5277 Views
Sep 13, 2021
4550 Views
May 29, 2021
5281 Views
Apr 27, 2021
3542 Views
Apr 06, 2021
5195 Views
Apr 03, 2021
4003 Views
Mar 26, 2021
2651 Views
Mar 19, 2021
3444 Views
Mar 19, 2021
2736 Views
Mar 05, 2021
2769 Views
Feb 18, 2021
3831 Views
Feb 18, 2021
3221 Views
Feb 11, 2021
2778 Views
Nov 10, 2020
3021 Views
Sep 09, 2020
4312 Views
Aug 24, 2020
4691 Views
Aug 20, 2020
4295 Views
Aug 18, 2020
5874 Views
Jul 10, 2020
8151 Views
Jul 07, 2020
11795 Views
Jul 07, 2020
10007 Views
Jul 07, 2020
12622 Views
Jul 07, 2020
8283 Views
Jul 04, 2020
18290 Views
Jul 04, 2020
9628 Views
Jul 04, 2020
6681 Views
Jul 04, 2020
3393 Views
Jul 03, 2020
9773 Views
Jul 01, 2020
8026 Views
Jul 01, 2020
5661 Views
Jun 30, 2020
13512 Views
Jun 30, 2020
10337 Views
Jun 23, 2020
7624 Views
Jun 17, 2020
7848 Views
Jun 16, 2020
5828 Views
Jun 12, 2020
9179 Views
Jun 09, 2020
6993 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.