CRUD App in PHP

CRUD App in PHP

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.

<?php
$servername = "localhost";
$username = "root";
$password = "";
$database="crud_app";

// Create connection
$conn = new mysqli($servername, $username, $password,$database);

// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
// echo "Connected successfully";
?>

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.

<?php
include "./db-conn.php";

$name = $_REQUEST["fullname"];
$email = $_REQUEST["email"];
$phone = $_REQUEST["phone"];

$sql = "INSERT INTO contacts(name,email,phone) VALUES ('$name', '$email','$phone')";

if ($conn->query($sql) === TRUE) {
    header("Location: ./index.php");
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

Here, I have mentioned the field names I have used in my database table.

How to Read/View Records?

The second operation, just as the name suggests, ‘Read’ is used to display or read the data that is already available in the database. For that, we will create another file named index.php which will display the records in the form of a table and there will be a form that will take the user’s input.

<?php
    $sql = "SELECT * FROM `contacts` ORDER BY reg_date DESC";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
      // output data of each row

      while ($row = $result->fetch_assoc()) {
        echo "<tr>
        <td>" . $row["id"] . "</td>
        <td>" . $row["name"] . "</td>
        <td>" . $row["email"] . "</td>
        <td>" . $row["phone"] . "</td>
        <td>" . $row["reg_date"] . "</td>
        <td>" .
          "<a href='./update.php'>Update </a> " .
          "<a href='./delete.php'>Delete </a>
        </td>
        </tr>";
      }
    } else {
      echo "0 results";
    }
    ?>

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

<?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);
$stmt->execute();

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

$stmt->close();
$conn->close();

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 : https://github.com/itslatikajoshi/lj-crud-app

Leave A Comment