Create Simple API CRUD with PHP and MySQL

In this post we have discussed how can do all this crud operation using php api. So, here we have created Simple PHP RESTful API step by step.





Our Best Popular Projects & Posts

Post Link
School Management System With Full Source Code In php click here
ecommerce project in php click here
simple login and registration form in php click here
Synopsis for online rooms booking project based on PHP click here
add multiple images in codeigniter click here
beginners project in php with source code click here
dynamic country state dropdown in codeigniter click here
how to generate pdf using dompdf in codeigniter click here
add 1 day to current date in php click here
how to check website/blog responsive or not? click here
employee attendance management system project in php with source code click here
Select one checkbox and disable others click here
printing div content with css applied click here
best restaurants management system in php click here
Preview an image before it is uploaded using jquery click here
text editor in jquery click here
data table with export button in html click here
add multiple images in codeigniter click here
codeigniter some steps to install on Your server click here
ajax in php/codeigniter click here
Create Simple API CRUD with PHP and MySQL click here
How to integrate bootstrap theme in codeigniter click here
how to prepare for interview click here
Convert a date format in PHP click here
how to export html table to excel in jquery click here
how to create duplicate google peg using ajax click here
how to make dropdown searchable in html using jquery click here
how to find retirement date in php click here
online food order system in php with source code click here
school management project admin panel in php click here






Data Base Scripy


SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";




CREATE TABLE `tbl_reg` (
  `id` int(100) NOT NULL,
  `name` varchar(100) NOT NULL,
  `email` varchar(100) NOT NULL,
  `password` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


INSERT INTO `tbl_reg` (`id`, `name`, `email`, `password`) VALUES
(1, 'dipesh', 'admin@admin.com', '11111'),
(2, 'dipesh', 'user@admin.com', '12345'),
(3, 'dipesh', 'dipesh,badiya', '12345'),
(4, 'anshul', 'anshulbaradiya', '12345'),
(5, 'aayush', 'ayush', '12345');


ALTER TABLE `tbl_reg`
  ADD PRIMARY KEY (`id`);


ALTER TABLE `tbl_reg`
  MODIFY `id` int(100) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=7;


COMMIT;

 

 

 

PHP Script

 <?php
 @$database=mysqli_connect("localhost","root","","test");
if (!$database) {
die("Connection failed: " . mysqli_connect_error());
}
class Inventory_api {
function __construct() {
}
public function test()
{
    echo "Welcome";
}
public function add_data()
{
global $database;
$name=$_POST["name"];
$email=$_POST["email"];
$password=$_POST["password"];
if($name!='' && $email!='' && $password!='')
{
$q="insert into tbl_reg(name,password,email) values('$name','$password','$email')";
mysqli_query($database,$q);
$n=mysqli_affected_rows($database);
if($n)
{

echo  json_encode(array('msg'=>'Inserted successfully','status'=>'1'));  
} }
else{ echo   json_encode(array('msg'=>'Not Inserted','status'=>'0')); }
}

public function log_in()
{
global $database;
$email=$_POST["email"];
$password=$_POST["password"];
if($email!='' && $password!='')
{
$q="SELECT * FROM tbl_reg where email = '$email' and password='$password'";
$result= mysqli_query($database,$q);
$r = mysqli_num_rows($result);
 if($r)
 {
  $Alldata=array();
  while ($row = mysqli_fetch_array($result)){  
$All['name'] =   $row['name'];
$All['password'] = $row['password'];
$All['email'] =   $row['email'];
$All['id'] =   $row['id'];
$Alldata[]=$All;
}
                
if($Alldata > 0)
{
echo json_encode(array('data'=>$Alldata,'msg' => 'Data Get Successfully','status'=>'1'));
}
}
else
{
echo json_encode(array('msg' =>'Data not found','status'=>'0'));
}}

}

public function show_data()
{
global $database;
$tab = 'tbl_reg';
$query="select * from tbl_reg";
$result= mysqli_query($database,$query);
$All=array();
while ($row = mysqli_fetch_array($result)){  
$All['name'] =   $row['name'];
$All['password'] = $row['password'];
$All['email'] =   $row['email'];
$Alldata[]=$All;
}
if($Alldata > 0)
          {
echo json_encode(array('data'=>$Alldata,'msg' => 'Data Get Successfully','status'=>'1'));
           // echo json_encode($arr);
}
else
{
echo json_encode(array('msg' =>'Data not found','status'=>'0'));
}
  //echo json_encode($arr);
  }


public function edit_data()
  {
global $database;
$id = $_POST['id'];
 $q="SELECT * FROM tbl_reg where id = '$id'";
$result= mysqli_query($database,$q);
$r = mysqli_num_rows($result);


 if($r)
 {
  $Alldata=array();
  while ($row = mysqli_fetch_array($result)){  
$All['name'] =   $row['name'];
$All['password'] = $row['password'];
$All['email'] =   $row['email'];

$Alldata[]=$All;
}
       

if($Alldata > 0)
{
echo json_encode(array('data'=>$Alldata,'msg' => 'Data Get Successfully', 'id'=>$id, 'status'=>'1'));
}
else
{
echo json_encode(array('msg' =>'Data not found','status'=>'0'));
         // echo json_encode($arr);
}
  //echo json_encode($arr);
}}

public function update_data()
{
global $database;

$name=$_POST["name"];
$email=$_POST["email"];
$password=$_POST["password"];
$id=$_POST["id"];

if($name!='' && $email!='' && $password!='' && $id!='')
{
$q="update tbl_reg set name='$name',password='$password',email='$email' where id='$id'";
mysqli_query($database,$q);
$n=mysqli_affected_rows($database);
if($n)
{

echo  json_encode(array('msg'=>'update successfully','status'=>'1'));  
}
else{ echo   json_encode(array('msg'=>' update Not Inserted','status'=>'0')); }
}}

public function delete_data()
{
    global $database;
  $id=$_POST["id"];

  $q="delete from tbl_reg where id='$id'";
mysqli_query($database,$q);
$n=mysqli_affected_rows($database);
if($n)

 {

echo  json_encode(array('msg'=>'Delete successfully','status'=>'1'));  
}
else{ echo   json_encode(array('msg'=>' Something went wrong','status'=>'0')); }
}

}



$view=$_GET["method"];
if(isset($_GET["method"]))
   $view = $_GET["method"];

 $obj = new Inventory_api();

switch($view){


case "add_data":
$obj->add_data();
break;

case "test":
$obj->test();
break;

case "show_data":
$obj->show_data();
break;

case "edit_data":
$obj->edit_data();
break;

case "update_data":
 $obj->update_data();
break;

case "delete_data":
$obj->delete_data();
break;

case "log_in":
$obj->log_in();
break;



case "" :
break;
}
    

?>

 

 API Url


localhost/pstmaan/Inventory_api.php/test?method=add_data

localhost/pstmaan/Inventory_api.php/test?method=delete_data

localhost/pstmaan/Inventory_api.php/test?method=show_data

localhost/pstmaan/Inventory_api.php/test?method=edit_data

localhost/pstmaan/Inventory_api.php/test?method=update_data

localhost/pstmaan/Inventory_api.php/test?method=log_in

 

 

0 Comments