PHP OOPS Dynamically Insert Update Delete MySQLi Query

PHP OOPS Dynamically Insert Update Delete MySQLi Query

In this Post We Will Explain About is PHP OOPS Dynamically Insert Update Delete MySQLi Query With Example and Demo.Welcome on Live24u.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to mysql – PHP OOPs dynamic insert update delete data Example

In this post we will show you Best way to implement Dynamically insert, update, delete in PHP with OOPs, hear for Dynamically Insert and Update Values In a MySQL Database Using OOP with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.




PHP OOPs dynamic insert update delete data

PHP OOPs dynamic insert Query using mysqli

	public function insert_data($tablename,$dataArray)
	{
		try
		{
			
			$getColumnsKeys = array_keys($dataArray);
			$implodeColumnKeys = implode(",",$getColumnsKeys);
			
			$getValues = array_values($dataArray);
			$implodeValues = "'".implode("','",$getValues)."'";
			
			$stmt = $this->conn->prepare("insert into $tablename (".$implodeColumnKeys.") values (".$implodeValues.")");
			$stmt->execute();	
			return $stmt;
		}
		catch(PDOException $ex)
		{
			echo $ex->getMessage();
		}
	}

Usage : dynamic Insert Query using mysqli


session_start();
require_once 'class.user.php';
$user_home = new USER();


$table_name = "erp_item_mst";
$cr_date = date("Y-m-d H:i:s");
if(isset($_POST['btn-add-item']))
{
	$item_name = trim($_POST['item_name']);
	$item_note = trim($_POST['item_note']);
	$data = array(
	"item_name" => $item_name,
	"item_note" => $item_note,
	"cr_date" => $cr_date
	);
	
	$stmt = $user_home->runQuery("SELECT * FROM $table_name WHERE item_name=:item_name and status=0");
	$stmt->execute(array(":item_name"=>$item_name));
	$row = $stmt->fetch(PDO::FETCH_ASSOC);
	
	if($stmt->rowCount() > 0)
	{
		$msg = "
		      <div class='alert alert-warning'>
				<button class='close' data-dismiss='alert'>×</button>
					<strong>Sorry !</strong>  ItemName allready exists , Please Try another one
			  </div>
			  ";
	}
	else
	{
		if($user_home->insert_data($table_name, $data))
		{			
			
			$msg = "
		      <div class='alert alert-success'>
				<button class='close' data-dismiss='alert'>×</button>
					<strong>Success !</strong>Record added successfully
			  </div>
			  ";
			  ?>
			  <script>
			  setTimeout(function () {
				   window.location.href= 'item_list.php'; // the redirect goes here

				},2000); // 5 seconds
			  </script>
			  <?php
		}
		else
		{
			echo "sorry , Query could no execute...";
		}		
	}
}

Dynamically UPDATE Query

PHP OOPs dynamic Update Query using mysqli

	public function update_data($tablename,$dataArray,$update_id)
	{
		try
		{
			$update_id = trim($update_id);
			$cols = array();
 
			foreach($dataArray as $key=>$val) {
				$cols[] = "$key = '$val'";
			}
			
			$stmt = $this->conn->prepare("UPDATE $tablename SET " . implode(', ', $cols) . " WHERE $update_id");
			$stmt->execute();	
			return $stmt;
		}
		catch(PDOException $ex)
		{
			echo $ex->getMessage();
		}
	}

Usage : dynamic Update Query using mysqli
session_start();
require_once 'class.user.php';
$user_home = new USER();

if(isset($_POST['btn-add-item-update']))
{
	$item_name = trim($_POST['item_name']);
	$item_note = trim($_POST['item_note']);
	$item_id = trim($_POST['item_id']);
	$update_id = "item_id = '$item_id'";
	$data = array(
	"item_name" => $item_name,
	"item_note" => $item_note,
	"cr_date" => $cr_date
	);
	
	$stmt = $user_home->runQuery("SELECT * FROM $table_name WHERE item_name=:item_name and status=0");
	$stmt->execute(array(":item_name"=>$item_name));
	$row = $stmt->fetch(PDO::FETCH_ASSOC);
	
	if($stmt->rowCount() > 1)
	{
		$msg = "
		      <div class='alert alert-warning'>
				<button class='close' data-dismiss='alert'>×</button>
					<strong>Sorry !</strong>  ItemName allready exists , Please Try another one
			  </div>
			  ";
	}
	else
	{
		if($user_home->update_data($table_name, $data,$update_id))
		{			
			
			$msg = "
		      <div class='alert alert-success'>
				<button class='close' data-dismiss='alert'>×</button>
					<strong>Success !</strong>Record Updated successfully.
			  </div>
			  ";
			  ?>
			  <script>
			  setTimeout(function () {
				   window.location.href= 'item_list.php'; // the redirect goes here

				},2000); // 5 seconds
			  </script>
			  <?php
		}
		else
		{
			echo "sorry , Query could no execute...";
		}		
	}
}

Dynamically Delete Query

PHP OOPs dynamic Delete Query using mysqli

	public function delete_data($tablename,$key,$id)
	{
		$stmt = $this->conn->prepare("update $tablename SET status=1 where $key = $id");
		$stmt->execute();	
		return $stmt;
	}	

Usage : dynamic Delete Query using mysqli

session_start();
require_once 'class.user.php';
$user_home = new USER();
if(isset($_GET['itemid']))
{
	$itemid = trim($_GET['itemid']);
	$tablename = "erp_item_mst";
	$key = "item_id";
	if($user_home->delete_data($tablename,$key,$itemid))
	{	
		echo "1";
	}
}

Create Dynamic SQL Insert Query in PHP and MySql

protected function dynemic_insert_data($erp_table_name, $lstdataarr) {
    $dtkey = array_keys($lstdataarr);
    $myval = array_values($lstdataarr);
    $livesql = "INSERT INTO $erp_table_name (" . implode(', ', $dtkey) . ") "
         . "VALUES ('" . implode("', '", $myval) . "')";
 
    return($livesql);
}

Dynamic SQL Insert Query in PHP and MySQL

Simple Syntex of the Dynamic SQL Insert Query in PHP and MySQL

mysqli_query($db_conn, "INSERT INTO YOUR_TABLE_NAME (collumn1,collumn2, ,…,collumn15,collumn16) VALUES ('".$dataval1."', '".$dataval2."', …,'".$dataval15."', '".$dataval16."'");

Sample dynamic insert query:

$erp_table_name is to store user table name of here database in which dataVal will be added or inserted.

Also Read :   AngularJS Login Script using PHP MySQL

$dataArray is the main variable which will data store user information what will be added or insert in database.
here sample example of the Set $dataArray array to be added inserted in mysql database

$dataArray = array(
"name" => "jaydeep",
"age" => "26",
"city" => "USA"
);

Dynamic Insert query (Function)

<?php 
/*
*
* Reusable source code for dynamic insert query in mysql database
*
*/
function dynemic_insert_data( $erp_table_name, $dataArray )
{
$key = array_keys($dataArray); 

$dataVal = array_values($dataArray); 

$query ="INSERT INTO $erp_table_name ( ". implode(',' , $key) .") VALUES('". implode("','" , $dataVal) ."')";

return $query;
}
?>

Usage Function

<?php 
//Your DB Config
$db_conn = mysqli_connect("localhost","UserName","YourPass", "DB Name(live24u)");

$erp_table_name = "user_mst";

$dataArray = array(
"name" => "jaydeep",
"age" => "26",
"city" => "USA"
);

$sql = dynemic_insert_data( $erp_table_name, $dataArray );

if(mysqli_query($db_conn, $sql))
{
echo "Success!, User record added successfully";
}
else
{
echo "Sorry!, Record not added !";
}
?>

You are Most welcome in my youtube Channel Please subscribe my channel. and give me FeedBack.
More Details……
Angularjs Example

Also Read :   Insert Update Delete Query in SQL - SQL CRUD Query





Example

I hope you have Got What is dynamic Update or Edit Data from Mysql Table using OOPS in PHP And how it works.I would Like to have FeedBack From My Blog(live24u.com) readers.Your Valuable FeedBack,Any Question,or any Comments about This Article(live24u.com) Are Most Always Welcome.