PHP CRUD Create, edit, update and delete Records with MySQL database

here We learn to step by step simple crud operations in php using mysqli with full source code Example.

PHP MySQL CRUD Application

we have a create a simple 4 files like as bellow.

  • index.php
  • script.js
  • school/database.php
  • school/save.php

PHP and MySqli CRUD Operation Tutorial
PHP and MySqli CRUD Operation Tutorial

Also Read: PHP Jquery Ajax CRUD Example Tutorial From Scratch

Step 1: Html View File

index.php





	
	
	
	PHP CRUDs Example - www.pakainfo.com
	
	
	
	
	
	
	
	


    
	
	
	
	
	
	


    

Reusable PHP CRUD DATABASE FUNCTIONS
Reusable PHP CRUD DATABASE FUNCTIONS

Also Read: PHP – MySQLi Insert Update Delete CRUDs Operation

script.js


Step 2: Connect Database

school/database.php


Also Read: PHP MySQL CRUDs Create, Insert, Update and Delete operations

Step 3: Save/Store Data in MySQL Database

school/save.php

 0){
	if($_POST['type']==1){
		$studnm=$_POST['studnm'];
		$studemail=$_POST['studemail'];
		$mobile=$_POST['mobile'];
		$address=$_POST['address'];
		$sql = "INSERT INTO `students`( `studnm`, `studemail`,`mobile`,`address`) 
		VALUES ('$studnm','$studemail','$mobile','$address')";
		if (mysqli_query($conn, $sql)) {
			echo json_encode(array("statusCode"=>200));
		} 
		else {
			echo "Error: " . $sql . "
" . mysqli_error($conn); } mysqli_close($conn); } } if(count($_POST)>0){ if($_POST['type']==2){ $id=$_POST['id']; $studnm=$_POST['studnm']; $studemail=$_POST['studemail']; $mobile=$_POST['mobile']; $address=$_POST['address']; $sql = "UPDATE `students` SET `studnm`='$studnm',`studemail`='$studemail',`mobile`='$mobile',`address`='$address' WHERE id=$id"; if (mysqli_query($conn, $sql)) { echo json_encode(array("statusCode"=>200)); } else { echo "Error: " . $sql . "
" . mysqli_error($conn); } mysqli_close($conn); } } if(count($_POST)>0){ if($_POST['type']==3){ $id=$_POST['id']; $sql = "DELETE FROM `students` WHERE id=$id "; if (mysqli_query($conn, $sql)) { echo $id; } else { echo "Error: " . $sql . "
" . mysqli_error($conn); } mysqli_close($conn); } } if(count($_POST)>0){ if($_POST['type']==4){ $id=$_POST['id']; $sql = "DELETE FROM students WHERE id in ($id)"; if (mysqli_query($conn, $sql)) { echo $id; } else { echo "Error: " . $sql . "
" . mysqli_error($conn); } mysqli_close($conn); } } ?>

Leave a Comment