Back to Roadmap
6:00

CRUD Operations in PHP

Learn how to create, read, update, and delete database records using PHP and MySQL

6 MIN READ VERIFIED CURRICULUM

CRUD operations are the foundation of most database-driven web applications.

PHP uses SQL queries and database connections to perform Create, Read, Update, and Delete operations on MySQL databases.

What is CRUD?

CRUD stands for Create, Read, Update, and Delete.

These operations allow applications to manage and manipulate data stored in databases.

Why CRUD Operations Matter

Most applications such as blogs, e-commerce websites, and dashboards rely on CRUD functionality.

CRUD operations enable users to add new records, view data, edit information, and remove unwanted entries.

Setting Up the Database Connection

Before performing CRUD operations, PHP must establish a connection with the MySQL database.

<?php
 $conn = mysqli_connect('localhost', 'root', '', 'company_db');

 if (!$conn) {
   die('Connection Failed');
 }
?>
php

Creating a Table

A database table is required to store records for CRUD operations.

CREATE TABLE employees (
 id INT AUTO_INCREMENT PRIMARY KEY,
 name VARCHAR(100),
 email VARCHAR(100)
);
sql

Create Operation

The Create operation inserts new records into a database table.

PHP uses SQL INSERT queries to add new data.

<?php
 $sql = "INSERT INTO employees(name, email) VALUES('Rahul', 'rahul@example.com')";
 mysqli_query($conn, $sql);
?>
php

Reading Records

The Read operation retrieves data stored in the database.

SELECT queries are used to fetch records from database tables.

<?php
 $sql = "SELECT * FROM employees";
 $result = mysqli_query($conn, $sql);
?>
php

Displaying Database Records

Fetched records are commonly displayed using loops.

<?php
 while($row = mysqli_fetch_assoc($result)) {
   echo $row['name'];
 }
?>
php

Update Operation

The Update operation modifies existing records inside the database.

UPDATE queries are used to change stored information.

<?php
 $sql = "UPDATE employees SET email='newmail@example.com' WHERE id=1";
 mysqli_query($conn, $sql);
?>
php

Delete Operation

The Delete operation removes records from database tables.

<?php
 $sql = "DELETE FROM employees WHERE id=1";
 mysqli_query($conn, $sql);
?>
php

Using HTML Forms with CRUD

HTML forms are commonly used to collect user input for CRUD operations.

<form method="POST">
  <input type="text" name="name" placeholder="Enter Name">
  <button type="submit">Save</button>
</form>
html

Handling Form Data

PHP retrieves submitted form values using superglobal arrays such as $_POST.

<?php
 $name = $_POST['name'];
?>
php

Prepared Statements

Prepared statements improve security by preventing SQL injection attacks.

User input should never be inserted directly into SQL queries.

<?php
 $stmt = $conn->prepare('INSERT INTO employees(name, email) VALUES(?, ?)');
 $stmt->bind_param('ss', $name, $email);
 $stmt->execute();
?>
php

Error Handling

Database errors should be handled properly to improve debugging and application reliability.

<?php
 if(mysqli_query($conn, $sql)) {
   echo 'Query Executed';
 } else {
   echo mysqli_error($conn);
 }
?>
php

Closing the Database Connection

Unused database connections should be closed after operations are completed.

<?php
 mysqli_close($conn);
?>
php

Best Practices

Always validate and sanitize user input before performing database operations.

Use prepared statements and proper error handling for secure and reliable applications.

Common Beginner Mistakes

Common mistakes include incorrect SQL syntax, forgetting WHERE clauses, and exposing database credentials publicly.

Developers should also avoid executing SQL queries without validating user input.

Real-World Use Cases

CRUD functionality is used in user management systems, blog platforms, inventory systems, and e-commerce applications.

Summary

CRUD operations allow PHP applications to create, retrieve, update, and delete database records efficiently.

Mastering CRUD functionality is essential for building dynamic and database-driven PHP applications.