PHP : REST API Insert / store, Update, Delete and list records

In this tutorial learn how to create an REST API with insert/store,update,delete and list records for android or IOS/iphone application using php api,let's go with step by step.

API tutorial

STEP 1 : create table in database, if you don't have database please create it here demo database name "lsapi".



 CREATE TABLE IF NOT EXISTS `categories` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
  `details` text COLLATE utf8mb4_unicode_ci NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci AUTO_INCREMENT=2 ;


STEP 2 : Create dbhelper.php  file in folder of api and setup your mysql username and password with database



 <?php

class Dbhelper
{
    public function dbconnect()
    {
       $servername = "localhost";
       $username = "root";
       $password = "root";
       $dbname = "lsapi";
       $conn = new mysqli($servername, $username, $password, $dbname);
       return $conn;
    }
    public function SelQuery($sql){
      $conn = $this->dbconnect();
      $result = mysqli_query($conn, $sql);
      return $result;
    }
    public function StoreQuery($sql){
      $conn = $this->dbconnect();
      $result = mysqli_query($conn, $sql);
      return $result;
    }
}

?>


STEP 3 : create category.php in api folder, here pass parameter token(api key),type(for store,update and delete).




 <?php
/*
*
*/
// Create a new object
require_once('dbhelper.php');

$obj = new Category;
$token = trim($_GET['token']);  //here you can post method for security
$type = trim($_POST['type']);  //type store, edit, update, delete

if($token=="12345" && $type == ""){
  echo $obj->index();
}
if($token=="12345" && $type == "store"){
  echo $obj->store();
}
if($token=="12345" && $type == "update"){
  echo $obj->update();
}
if($token=="12345" && $type == "delete"){
  echo $obj->delete();
}

class Category
{
    public function index()
    {
       $dbhelper = new Dbhelper();
       $sql = "SELECT * FROM categories";
       $res = $dbhelper->SelQuery($sql);
       $result = array();
       if (mysqli_num_rows($res) > 0) {
         $data = array();
         while ($row = $res->fetch_assoc()) {
           $det=array();
           $det ['id'] = $row['id'];
           $det ['name'] = $row['name'];
           $det['details'] = $row['details'];
           $data[]=$det;
         }
         $result ['category']=$data;
         $result ['status']=1;
       }else{
        $result ['error']="Categories are not available";
        $result ['status']=0;
      }
      echo json_encode($result);
    }
    public function store(){
      $dbhelper = new Dbhelper();
      $name = $_POST['name'];
      $details = $_POST['details'];
      $result = array();
      if($name!='' && $details!=''){
        $sql = "INSERT INTO  `categories` (`id` ,  `name` ,  `details` ,  `created_at` ,  `updated_at`)
        VALUES (NULL ,'".$name."','".$details."', NULL , NULL)";
        $res = $dbhelper->StoreQuery($sql);
      }
      if($res){
        $result ['msg']="Successfully Inserted data!";
        $result ['status']=1;
      }else{
        $result ['error']="Not Inserted Data!";
        $result ['status']=0;
      }
      echo json_encode($result);
    }
    public function update(){
      $dbhelper = new Dbhelper();
      $id = $_POST['id'];
      $name = $_POST['name'];
      $details = $_POST['details'];

      $result = array();
      if($name!='' && $details!='' && $id!=''){
        $sql = "UPDATE `categories` SET `name`='".$name."',`details`='".$details."' WHERE `id`='".$id."'";
        $res = $dbhelper->StoreQuery($sql);
      }
      if($res){
        $result ['msg']="Successfully Updated data!";
        $result ['status']=1;
      }else{
        $result ['error']="Not Update Data!";
        $result ['status']=0;
      }
      echo json_encode($result);
    }

    public function delete(){
      $dbhelper = new Dbhelper();
      $id = $_POST['id'];
      $result = array();
      if($id!=''){
        $sql = "DELETE FROM `categories` WHERE `id`='".$id."'";
        $res = $dbhelper->StoreQuery($sql);
      }
      if($res){
        $result ['msg']="Successfully Deleted data!";
        $result ['status']=1;
      }else{
        $result ['error']="Not deleted Data!";
        $result ['status']=0;
      }
      echo json_encode($result);
    }
}
?>

STEP 4 : api url call in your application


http://localhost/api/category.php?token=12345
STEP 5 : Records list display in JSON



 {"category":[{"id":"5","name":"Fruit","details":"This is Fruit test"},{"id":"6","name":"Fruit","details":"This is Fruit test"},{"id":"7","name":"Cloth","details":"Shirt, Pants etc"},{"id":"8","name":"Cloth","details":"Shirt, Pants etc"}],"status":1}

HERE ARE THE SCREENSHOT ATTACHED How to check with postsman.

1. List All Records




2. Store data with API


3. Update Data with API





4. DELETE Record with API passing id using post method




No comments:

Post a Comment

how to call ssh from vs code

 To call SSH from VS Code, you can use the built-in Remote Development extension. This extension allows you to open a remote folder or works...