Example 1: php oop crud database
<?php
class Customers
{
private $servername = "localhost";
private $username = "root";
private $password = "";
private $database = "blog_database";
public $con;
public function __construct()
{
$this->con = new mysqli($this->servername, $this->username,$this->password,$this->database);
if(mysqli_connect_error()) {
trigger_error("Failed to connect to MySQL: " . mysqli_connect_error());
}else{
return $this->con;
}
}
public function insertData($post)
{
$name = $this->con->real_escape_string($_POST['name']);
$email = $this->con->real_escape_string($_POST['email']);
$username = $this->con->real_escape_string($_POST['username']);
$password = $this->con->real_escape_string(md5($_POST['password']));
$query="INSERT INTO customers(name,email,username,password) VALUES('$name','$email','$username','$password')";
$sql = $this->con->query($query);
if ($sql==true) {
header("Location:index.php?msg1=insert");
}else{
echo "Registration failed try again!";
}
}
public function displayData()
{
$query = "SELECT * FROM customers";
$result = $this->con->query($query);
if ($result->num_rows > 0) {
$data = array();
while ($row = $result->fetch_assoc()) {
$data[] = $row;
}
return $data;
}else{
echo "No found records";
}
}
public function displyaRecordById($id)
{
$query = "SELECT * FROM customers WHERE id = '$id'";
$result = $this->con->query($query);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
return $row;
}else{
echo "Record not found";
}
}
public function updateRecord($postData)
{
$name = $this->con->real_escape_string($_POST['uname']);
$email = $this->con->real_escape_string($_POST['uemail']);
$username = $this->con->real_escape_string($_POST['upname']);
$id = $this->con->real_escape_string($_POST['id']);
if (!empty($id) && !empty($postData)) {
$query = "UPDATE customers SET name = '$name', email = '$email', username = '$username' WHERE id = '$id'";
$sql = $this->con->query($query);
if ($sql==true) {
header("Location:index.php?msg2=update");
}else{
echo "Registration updated failed try again!";
}
}
}
public function deleteRecord($id)
{
$query = "DELETE FROM customers WHERE id = '$id'";
$sql = $this->con->query($query);
if ($sql==true) {
header("Location:index.php?msg3=delete");
}else{
echo "Record does not delete try again";
}
}
}
?>
Example 2: ajax php crud example
$(document).on('click','#btn-add',function(e) {
var data = $("#user_form").serialize();
$.ajax({
data: data,
type: "post",
url: "backend/save.php",
success: function(dataResult){
var dataResult = JSON.parse(dataResult);
if(dataResult.statusCode==200){
$('#addEmployeeModal').modal('hide');
alert('Data added successfully !');
location.reload();
}
else if(dataResult.statusCode==201){
alert(dataResult);
}
}
});
});
$(document).on('click','.update',function(e) {
var id=$(this).attr("data-id");
var name=$(this).attr("data-name");
var email=$(this).attr("data-email");
var phone=$(this).attr("data-phone");
var city=$(this).attr("data-city");
$('#id_u').val(id);
$('#name_u').val(name);
$('#email_u').val(email);
$('#phone_u').val(phone);
$('#city_u').val(city);
});
$(document).on('click','#update',function(e) {
var data = $("#update_form").serialize();
$.ajax({
data: data,
type: "post",
url: "backend/save.php",
success: function(dataResult){
var dataResult = JSON.parse(dataResult);
if(dataResult.statusCode==200){
$('#editEmployeeModal').modal('hide');
alert('Data updated successfully !');
location.reload();
}
else if(dataResult.statusCode==201){
alert(dataResult);
}
}
});
});
$(document).on("click", ".delete", function() {
var id=$(this).attr("data-id");
$('#id_d').val(id);
});
$(document).on("click", "#delete", function() {
$.ajax({
url: "backend/save.php",
type: "POST",
cache: false,
data:{
type:3,
id: $("#id_d").val()
},
success: function(dataResult){
$('#deleteEmployeeModal').modal('hide');
$("#"+dataResult).remove();
}
});
});
$(document).on("click", "#delete_multiple", function() {
var user = [];
$(".user_checkbox:checked").each(function() {
user.push($(this).data('user-id'));
});
if(user.length <=0) {
alert("Please select records.");
}
else {
WRN_PROFILE_DELETE = "Are you sure you want to delete "+(user.length>1?"these":"this")+" row?";
var checked = confirm(WRN_PROFILE_DELETE);
if(checked == true) {
var selected_values = user.join(",");
console.log(selected_values);
$.ajax({
type: "POST",
url: "backend/save.php",
cache:false,
data:{
type: 4,
id : selected_values
},
success: function(response) {
var ids = response.split(",");
for (var i=0; i < ids.length; i++ ) {
$("#"+ids[i]).remove();
}
}
});
}
}
});
$(document).ready(function(){
$('[data-toggle="tooltip"]').tooltip();
var checkbox = $('table tbody input[type="checkbox"]');
$("#selectAll").click(function(){
if(this.checked){
checkbox.each(function(){
this.checked = true;
});
} else{
checkbox.each(function(){
this.checked = false;
});
}
});
checkbox.click(function(){
if(!this.checked){
$("#selectAll").prop("checked", false);
}
});
});
Example 3: php oop crud database
-- Table structure for table `customers`
--
CREATE TABLE `customers` (
`id` int(100) NOT NULL,
`name` varchar(100) NOT NULL,
`email` varchar(100) NOT NULL,
`username` varchar(100) NOT NULL,
`password` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Example 4: crud operations in php
<?php
$personName = $_POST['personName'];
$address = $_POST['address'];
$mobile = $_POST['mobile'];
$email = $_POST['email'];
$message = $_POST['message'];
$tdate=new DateTime();
$sql = "INSERT INTO tbquery (name, email, mobile,address, comment, postdate)
VALUES ('". $personName."',
'". $email ."',
'". $mobile ."',
'". $address ."',
'". $message ."',
'". $tdate->format('Y-m-d') ."'
)";
if (mysqli_query($conn, $sql)) {
echo "Your query posted successfully";
} else {
echo "Error: " . $sql . "" . mysqli_error($conn);
}
mysqli_close($conn);
?>