CRUD Operation In PHP

📔 : PHP 🔗

In this article , we will implement a simple CRUD application in core PHP. So first of all , make directory crud in your htdocs or www folder. We will implement all files in this directory.

Note : Make a database with 'crud_db' and a 'user' table (fields : id int : auto increment , fname : varchar, lname : varchar, city : varchar, status : Boolean)

Now create adb.php file. In this file set your database credentials for you MySQL database. In this file we will use only database things so that we can reuse (using include) it whenever we need.

File Name : crud/db.php

Copy Fullscreen Close Fullscreen
<?php
  /*set your MySQL database credentials*/
  $conn = mysqli_connect("localhost","root","","crud_db");
  if(!$conn)
  {
    echo "Connection failed!";
    exit;
  }
?>

Next, create a index.php file , by default we will land on this page. Here we will list all the registered users that are going to be register via our PHP script. And if you see , there is a link to register a new user.

File Name : crud/index.php

Copy Fullscreen Close Fullscreen
<html>
  <head>
    <title>Home</title>
    <style type="text/css">
      table td {padding: 8px;}
    </style>
  </head>
  <body>
    <div id="header">
      <h1>CRUD</h1>
    </div>
    <div id="nav">
      <a href="crud.php">Home</a> | <a href="register.html">Register</a>
    </div>
    <h3>User details</h3>
    
    <?php
    /*first include database config file*/
    include_once('db.php');
    $sql = "select * from user where status=1";

    $result = mysqli_query($conn,$sql);

    echo "<table border='1'>";
    while($row = mysqli_fetch_assoc($result))
    {
      echo "<tr>";
      echo "<td>".$row['fname']."</td>";
      echo "<td>".$row['lname']."</td>";
      echo "<td>".$row['city']."</td>";
      echo "<td><a href='edit.php?id=".$row['id']."'>Edit</a></td>";
      echo "<td><a href='delete.php?id=".$row['id']."'>Delete</a></td>";
      echo "</tr>";
    }
    echo "</table>";
    ?>
  </body>
</html>

Now create a simple HTML file where we are going to make form with some input fields. This form will send all the info to the register.php file where we we will insert new user data.

File Name : crud/register.html

Copy Fullscreen Close Fullscreen
<html>
  <head>
    <title>Register User</title>
  </head>
  <body>
    <div id="header">
      <h1>CRUD Operation In PHP</h1>
    </div>
    <div id="nav">
      <a href="index.php">Home</a> | <a href="register.php">Register</a>
    </div>
    <h3>Register here</h3>
    <div id="data">
      <form action="register.php" method="post">
        <label>First name :</label> <br>
        <input type="text" name="fname"><br>
        <label>Last name :</label><br>
        <input type="text" name="lname"><br>
        <label>City :</label><br>
        <input type="text" name="city"><br><br>
        <input type="submit" value="Save">
      </form>
    </div>
  </body>
</html>

Here we will insert new user data in to the database , that is coming through the register.html file

File Name : crud/register.php

Copy Fullscreen Close Fullscreen
<?php
  /*first include database config file*/
  include_once('db.php');
  if(isset($_POST['fname']))
  {
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $city = $_POST['city'];
    /*SQL query to insert new user info.*/
    $sql = "insert into user(fname,lname,city,status) value('$fname','$lname','$city',1)";
    if(mysqli_query($conn,$sql))
    {
      /*if successfully inserted then redierct on listing page.*/
      header('Location:index.php');
    }
    else
    {
      /*print mysql error*/
      echo mysqli_error($conn);
    }
  }
?>

This is for editing the user info, to do so we have extract the user info from the database according to id field. We will send the update data to edit_update.php file where we will update the user info in database table.

File Name : crud/edit.php

Copy Fullscreen Close Fullscreen
<?php
  /*first include database config file*/
  include_once('db.php');
  if(isset($_GET['id']))
  {
  	$id = $_GET['id'];
  	$sql = "select * from user where id=$id and status=1";
  	$result = mysqli_query($conn,$sql);
  	$row = mysqli_fetch_assoc($result);
  }
?>
<html>
  <head>
     <title>Edit User</title>
  </head>
  <body>
    <div id="header">
      <h1>CRUD Operation In PHP</h1>
    </div>
    <div id="nav">
      <a href="index.php">Home</a> | <a href="register.html">Register</a>
    </div>
    <h3>Update <?php echo $row['fname'] .' '. $row['lname']?></h3>
    <div id="data">
      <form action="edit_update.php" method="post">
        <label>First name</label><br>
        <input type="text" name="fname" value="<?= $row['fname']?>"><br>
        <label>Last name</label><br>
        <input type="text" name="lname" value="<?= $row['lname']?>"><br>
        <label>City</label><br>
        <input type="text" name="city" value="<?= $row['city']?>"><br>
        <input type="hidden" name="id" value="<?= $row['id']?>"><br><br>
        <input type="submit" value="Update">
      </form>
    </div>
  </body>
</html>

Here we will update user data.

File Name : crud/edit_update.php

Copy Fullscreen Close Fullscreen
<?php
  /*first include database config file*/
  include_once('db.php');
  if(isset($_POST['fname']))
  {
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $city = $_POST['city'];
    $id = $_POST['id'];

    /*SQL query to update user data*/
    $sql = "update user set fname='$fname', lname='$lname', city='$city' where id=$id";
    if(mysqli_query($conn,$sql))
    {
      /*if successfully updated then redierct on listing page.*/
      echo header('Location:index.php');
    }
    else
    {
      /*print mysql error*/
      echo mysqli_error($conn);
    }
  }
?>

Create one more file delete.php to delete user info from the database.

File Name : crud/delete.php

Copy Fullscreen Close Fullscreen
<?php
  /*first include database config file*/
  include_once('db.php');
  if(isset($_GET['id']))
  {
    $id = $_GET['id'];
    $sql = "delete from user where id=$id";
    if(mysqli_query($conn,$sql))
    {
      /*now redirect on listing page*/
      header('Location:index.php');
    }
    else
    {
      echo mysqli_error($conn);
    }
  }
?>

Congrats ! you have made a CRUD operatio in PHP. To access it start you XAMPP/WAMPP Server , and hit localhost/crud .

Note : Don't forget to make a database with 'crud_db' and a 'user' table (fields : id int : auto increment , fname : varchar, lname : varchar, city : varchar, status : Boolean)

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! My name is Rahul Kumar Rajput. I'm a back end web developer and founder of learnhindituts.com. I live in Uttar Pradesh (UP), India and I love to talk about programming as well as writing technical tutorials and tips that can help to others.

Get connected with me. :) LinkedIn Twitter Instagram Facebook