PHP CRUD (Create, Read, Update, Delete) with MySQL Database with Video in Khmer
Master PHP CRUD (Create, Read, Update, Delete) with MySQL Database with step-by-step Khmer video tutorials and practical code examples.
2026-08-19 ยท 6 min read
Hi my friends! Today I will show you how to do PHP CRUD Operations with MySQL Database. Very simple explanations with code examples and video tutorials in Khmer!

What is CRUD?
- C = Create (Insert new data into database)
- R = Read (Select and show data from database)
- U = Update (Change or edit existing data)
- D = Delete (Remove data from database)
CRUD is the foundation for any website like e-commerce, blog, or user management system! Let's start step by step together.
1. Connect PHP with MySQL Database
First step, we must connect PHP script to MySQL database using mysqli. Check out the PHP MySQLi Official Documentation. If you haven't started XAMPP yet, see How to Download Install and Configure XAMPP!
Example db.php: Database connection script
<?php
$servername = "localhost"; // Database server host
$username = "root"; // MySQL username
$password = ""; // MySQL password
$database = "mydatabase";// Database name
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection success or failed
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected to MySQL successfully!";
?>
๐ก Tip: Put database connection code in separate file like
db.php, then userequire_once 'db.php';in other pages so you don't rewrite connection code again and again!
Watch video: How to connect PHP with MySQL Database in Khmer
2. Create Data (Insert Record)
To save new information into database table, we use SQL INSERT INTO statement.
Watch video: How to Insert data into MySQL database with Form in Khmer
Example 1: Insert hardcoded record
<?php
require_once "db.php";
$sql = "INSERT INTO users (name, email) VALUES ('Sok Dara', 'dara@example.com')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully!";
} else {
echo "Error inserting data: " . $conn->error;
}
?>
Example 2: Insert data from HTML form ($_POST)
<?php
require_once "db.php";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['name'];
$email = $_POST['email'];
$sql = "INSERT INTO users (name, email) VALUES ('$name', '$email')";
if ($conn->query($sql) === TRUE) {
echo "User registered successfully!";
}
}
?>
๐ก Tip: In production apps, never pass user input directly into
$sqlstring to prevent SQL Injection! Always use prepared statements$conn->prepare().
3. Read Data (Select & Display Records)
To retrieve and display data from MySQL database, we use SQL SELECT statement and loop through results using fetch_assoc().
Watch video: How to get data from MySQL and display in HTML Table in Khmer
Example 1: Fetch and echo text output
<?php
require_once "db.php";
$sql = "SELECT id, name, email FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "ID: " . $row["id"] . " | Name: " . $row["name"] . " | Email: " . $row["email"] . "<br>";
}
} else {
echo "No results found in database.";
}
?>
Example 2: Display data inside HTML Table
<?php
require_once "db.php";
$result = $conn->query("SELECT id, name, email FROM users");
?>
<table border="1">
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr>
<?php while($row = $result->fetch_assoc()): ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['email']; ?></td>
</tr>
<?php endwhile; ?>
</table>
๐ก Tip: Always check
$result->num_rows > 0before runningwhileloop so your web page doesn't crash or display empty tables when database has no records!
4. Update Data (Edit Record)
When user wants to change existing information in database, we use SQL UPDATE statement with WHERE id = ....
Example 1: Update user name by ID
<?php
require_once "db.php";
$sql = "UPDATE users SET name = 'Sok Sitha' WHERE id = 1";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully!";
} else {
echo "Error updating record: " . $conn->error;
}
?>
Example 2: Update multiple columns (Name and Email)
<?php
require_once "db.php";
$id = 2;
$name = "Bopha Vang";
$email = "bopha.updated@example.com";
$sql = "UPDATE users SET name = '$name', email = '$email' WHERE id = $id";
if ($conn->query($sql) === TRUE) {
echo "User profile updated successfully!";
}
?>
๐ก Tip: ALWAYS include
WHERE id = ...clause when writingUPDATEquery! If you forgetWHERE, it will update EVERY row in your database table!
Watch video: How to UPDATE data with Form in MySQL in Khmer
5. Delete Data (Remove or Soft Delete Record)
To delete data from database, we can use Hard Delete (DELETE FROM) or Soft Delete (UPDATE ... SET is_active = 0).
Example 1: Soft Delete (Recommended in real production apps)
<?php
require_once "db.php";
// Soft delete: We keep data record in DB, but mark status inactive/deleted
$sql = "UPDATE users SET is_active = 0 WHERE id = 1";
if ($conn->query($sql) === TRUE) {
echo "User account deactivated (Soft deleted) successfully!";
}
?>
Example 2: Hard Delete (Permanently remove row)
<?php
require_once "db.php";
$sql = "DELETE FROM users WHERE id = 1";
if ($conn->query($sql) === TRUE) {
echo "Record permanently deleted from database!";
} else {
echo "Error deleting record: " . $conn->error;
}
?>
๐ก Tip: Soft delete (
is_active = 0oris_deleted = 1) is safer thanDELETE FROMbecause if user clicks delete by accident, admin can restore the data!
Watch video: How to DELETE data from MySQL in PHP Khmer
6. Close Database Connection
After finishing database queries, always close connection to free server resources.
<?php
$conn->close();
?>
๐ก Tip: Closing database connection prevents your MySQL database from reaching
Too many connectionslimit on busy servers.
๐ก Next Lesson: Transform your CRUD skills into a REST API with How to Create Simple PHP API with Database & Test with Postman!
Hope you enjoy learning PHP CRUD operations with me! Don't forget to share this post with your friends. Happy coding! Sharing is caring!