March 4, 2024
CRUD app in php


In this blog, you’ll discover a step-by-step guide on creating a CRUD app in PHP, complete with MySQL integration. When learning a new programming language, mastering CRUD operations is fundamental. But what exactly does CRUD mean?

    • Create
    • Read
    • Update
    • Delete

The first step towards this is to make a database in your local server such as XAMPP. Type localhost/phpmyadmin in your browser URL. After this below screen will appear.

Create a new database for the CRUD app here or if you want to use an existing database click on the name of the database. Create a new table in the database. After the creation of the database and table, the next step is to create a folder inside xampp/htdocs with the name “crud-app” or you can choose a name according to your choice, next create a database connection between the database and your PHP code.

How to Create a MySQL Database Connection?

Create a file name db-conn.php.

The following code acts as the connection between the webpage and the database where the data from the webpage will be stored.

Change the username, password, and database name according to the name of them in your system.

How to Create Records?

The first operation in PHP CRUD Operations, Create, is responsible for creating tables or new records into an existing table. To do that, first, you must write the code for the webpage to create an entry in the database.

Create a file named create.php and insert the following code which will insert the form data to the backend.

How to Update Records?

The third operation i.e., ‘update’ is used to change or modify the already existing data present in the database.

To do this, you need to create another page to update the details in the database. Here, name the file as update.php

include "./db-conn.php";

$id = $_POST['id'];
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];

$sql= "UPDATE contacts SET name = ? email = ? phone = ?  WHERE id = ?";
*NOTE: ? is a placeholder for the new value of the specified column.

How to Delete Records?

The last operation of CRUD is Delete and just as the name suggests, it is used to delete an existing entry or table.

To perform this operation, you must create a page that would let you choose the data entry that you want to delete from the database.

Now, name the file delete.php

<?php include "./db-conn.php"; $id = $_GET['id']; $sql = "DELETE FROM contacts WHERE id = ?"; $stmt = $conn->prepare($sql);
$stmt->bind_param("i", $id);

if ($stmt->affected_rows > 0) {
    header("Location: /crud/");
    exit(); // Ensure no further execution of the script
} else {
    echo "Error deleting contact: " . $conn->error;


Conclusion :

With the conclusion of this “PHP CRUD Operations” tutorial, you’ve gained a comprehensive understanding of how to execute CRUD operations on a database using PHP. Throughout this tutorial, you’ve mastered the essential techniques for creating, reading, updating, and deleting records across various web pages.

To access the full code of this CRUD app you can visit GitHub link :

Leave A Comment