29
1
0
Insert, Update, Delete Data in MySQL with PHP

Insert, Update, Delete Data in MySQL with PHP

Published on July 3, 2025 by OBSCountdown Editorial

Insert, Update, Delete Data in MySQL with PHP

Learn how to manipulate MySQL database records using PHP and PDO securely and efficiently.

๐Ÿงฉ Why Learn CRUD Operations?

CRUD stands for Create, Read, Update, and Delete โ€” the four basic functions of persistent storage. In PHP, performing these actions with a MySQL database is essential for building dynamic applications.

๐Ÿ“ Insert Data

<?php
$pdo = new PDO("mysql:host=localhost;dbname=demo", "user", "pass");
$stmt = $pdo->prepare("INSERT INTO users (username, email) VALUES (:username, :email)");
$stmt->execute([
  'username' => 'john_doe',
  'email' => '[email protected]'
]);
echo "Data inserted successfully.";
?>

๐Ÿ” Update Data

<?php
$stmt = $pdo->prepare("UPDATE users SET email = :email WHERE username = :username");
$stmt->execute([
  'username' => 'john_doe',
  'email' => '[email protected]'
]);
echo "Data updated successfully.";
?>

๐Ÿ—‘๏ธ Delete Data

<?php
$stmt = $pdo->prepare("DELETE FROM users WHERE username = :username");
$stmt->execute(['username' => 'john_doe']);
echo "Data deleted successfully.";
?>

โœ… Best Practices

  • Use prepared statements to avoid SQL injection
  • Always validate and sanitize user input
  • Check affected rows with rowCount() if needed
  • Log changes if you're performing sensitive updates or deletions

๐Ÿงพ Summary

Performing insert, update, and delete operations in PHP using PDO is both secure and efficient. These CRUD operations are the backbone of most PHP-based web applications. Mastering them is key to becoming a confident backend developer.

Comments (0)

No comments yet. Be the first to comment!

Leave a Comment
Replying to someone's comment. Cancel
29
1
0
Join Our OBS Community

Loading...

Join Now