Monday, September 30, 2013

Simple and Flexible function in PHP for ADD and EDIT in Database Table

Hiii, we all know that using with functions in our code makes our workflow very easier and our work also gets easy. It is very good if we have the already built raw database functions for basic and simple tasks like database insertion and edit. So, here is the simple class and function to create fancy and flexible ADD and EDIT FUNCTIONS.
ADD and EDIT in Database Table


Here's the code :

1. FIrstly create a class (dbClass.php) for all database functions like add and edit  as shown below:
<?php
class dbScheme
{
/** database connection username*/
var $dbUser        =    "";
/**database connection password*/
var $dbPass        =    "";
/**database name*/
var $dbName        =    "";
/** database connection host*/
var $dbHost        =    "localhost";
/**database connection link*/
}
class dbClass extends dbScheme
{
/** database query execution result*/
var $res;
/**temporary variable for miscallenous purposes*/
var $temp;
/** temporary variable for miscallenous purposes*/
var $dbType        =    'mysql';
/** Buffer variable for setting the flag of query buffering.*/
var $dbBuffer    =    true;
/**Sql variable for sql statement.*/
var $sql;
/**database connection link*/
var $dbLink;
/**database selected Resource*/
var $dbConn;
/** Constructor of class*/
function dbClass( $dbName=NULL, $dbUser=NULL, $dbPass=NULL, $dbHost=NULL, $dbType=NULL )
{
$this->dbName    =    ( !empty( $dbName ) )?$dbName:$this->dbName;
$this->dbUser    =    ( !empty( $dbUser ) )?$dbUser:$this->dbUser;
$this->dbPass    =    ( !empty( $dbPass ) )?$dbPass:$this->dbPass;
$this->dbType    =    ( !empty( $dbType ) )?$dbType:$this->dbType;
$this->dbHost    =    ( !empty( $dbHost ) )?$dbHost:$this->dbHost;
}
/**  Function to connect to the database*/
function connect()
{
if( $this->dbType = 'mysql' )
{
//$this->dbLink    =    mysql_connect($this->dbHost,$this->dbUser,$this->dbPass,1,131072)  or die("Cannot connect with specified database".mysql_error());
$this->dbLink    =    mysql_pconnect($this->dbHost,$this->dbUser,$this->dbPass,131072)  or die("Cannot connect with specified database".mysql_error());
$this->dbConn    =    mysql_select_db( $this->dbName , $this->dbLink );
return $this->dbLink;
}
}
/**  Function to Disconnect to the database*/
function disconnect()
{
if( $this->dbType = 'mysql' )
{
mysql_close($this->dbLink);
}
}
/**  Function to execute a query in the database*/
function query( $sql )
{
$this->res    = mysql_query( $sql) or die( "Error in sql statement:<br/>".$sql."<br/>".mysql_error());
$this->temp        = mysql_insert_id();
return $this->res;
}
/**  Function to insert record in a database*/
function insert_into_table($table_name, $arr_fields){
if(count($arr_fields)>0){
$i = 0;
$sql    =    "insert into ".$table_name."( ";
foreach($arr_fields as $field => $field_value ){
$sql .= ($i==0)?"":",";
$sql .= $field;
$i++;
}
$sql .=") values(";
$i = 0;
foreach($arr_fields as $field => $field_value ){
$sql .= ($i==0)?"":",";
$sql .= " '".$field_value."' ";
$i++;
}
$sql .=")";
$this->query($sql);
}
return "Record Added Successfully";
}
/**  Function to update records in a database */
function update_table($table_name, $arr_fields, $primary_key, $primary_key_id){
if(count($arr_fields)>0 && $primary_key!="" && $primary_key_id!=""){
$i = 0;
$sql    =    "update ".$table_name." set ";
foreach($arr_fields as $field => $field_value ){
$sql .= ($i==0)?"":",";
$sql .= $field."= '".$field_value."' ";
$i++;
}
$sql .=" where ".$primary_key."='".$primary_key_id."' ";
$this->query($sql);
}
return "Record Updated Successfully";
}
}
?>
2. Then create  your custom class (in my case abc.php) extending (dbClass.php ) write the code given below :
<?php
class abc extends dbClass{
var $table_name;
/** Constructor to specify table name which will used in the whole document */
function abc($table_name)
{
$this->table_name     = $table_name;
}
/** Function to add/edit a record from a table */
function addEditabc($arr_fields, $primary_key_name, $primary_key_value)
{
if($primary_key_value!="" && $primary_key_value!=0 && !empty($primary_key_value))
{
$prompt = $this->update_table($this->table_name, $arr_fields, $primary_key_name, $primary_key_value);
}
else
{
$prompt = $this->insert_into_table($this->table_name, $arr_fields);
}
return $prompt;
}
}
?>
Note: If you are using this method, then you have to pass an array to addEditabc() function as parameter which must contain all the values/indexes with the same name as that of database table.

Hope you like this post……..Please Comment…………!!!!!!!!!


Tags: ,

0 Responses to “Simple and Flexible function in PHP for ADD and EDIT in Database Table”

Post a Comment

© 2013 MyCodeStock. All rights reserved.
Designed by SpicyTricks