0% found this document useful (0 votes)
207 views

Insert Update Delete Function

This document describes PHP functions for inserting, updating, and deleting records in a database table. The insert function builds an SQL query to insert the values of all POST fields into a database table. The update function builds a query to update the values of all POST fields in a table record matching a primary key. The delete function builds a query to delete a record from a table matching a primary key value. All functions return success or a MySQL error.

Uploaded by

Biplab Dutta
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
207 views

Insert Update Delete Function

This document describes PHP functions for inserting, updating, and deleting records in a database table. The insert function builds an SQL query to insert the values of all POST fields into a database table. The update function builds a query to update the values of all POST fields in a table record matching a primary key. The delete function builds a query to delete a record from a table matching a primary key value. All functions return success or a MySQL error.

Uploaded by

Biplab Dutta
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Insert/Update/Delete functions

Author : atdc98

How many times have your cursed PHP because you have to explicitly name each field and value when
inserting and updating your database fields? So long as your form field names are named the same as
your DB fields, this script will update, insert, and delete upon command. It's wonderfully commented,
too!!!

PHP Example :

<?php
//////////////////////////////////////////////////
//////////
// Function Name:    db_insert()
// Accepts:            varchar table
// Returns:            Success or mysql_error()
//
// Author:            Andrew Deering
// Date:            12/09/04
//
// Purpose:
//
//    This function takes all of the posted form e
lements 
//    and inserts them into $table.
//////////////////////////////////////////////////
/////////
function db_insert($table){
    //$query = "INSERT INTO <table>(
    $query = "INSERT INTO ".$table." (";
    
    
    //$query = "INSERT INTO <table>($fldnm1, $fldn
m2, ... $fldnmN
    //$fldnmN = $fieldnameN //EXPLANATION//
    $trigger = 0;
    foreach($_POST as $field => $value){
      if($trigger > 0) $query = $query . ", ";
      $query = $query . $field;
      $trigger++;
    }
    
    
    //$query = "INSERT INTO <table>(fldnm1, fldnm2
, ... fldnmN) VALUES(trim('
    $query = $query . ") VALUES (trim('";
    
    
    //$query = "INSERT INTO <table>(fldnm1, fldnm2
, ... fldnmN)    VALUES(trim('value1'), trim('valu
e2'), trim('valueN')
    $trigger = 0;
    foreach($_POST as $field => $value){
      if($trigger > 0) $query = $query . ", trim('
";
      $query = $query . $value."')";
      $trigger++;
    }
    
    
    //$query = "INSERT INTO <table>(fldnm1, fldnm2
, ... fldnmN) VALUES(trim('value1'), trim('value2'
), trim('valueN'));
    $query = $query . ");";
    
    
    //IF query runs, return.  Else, tell me why
    //echo $query."<br><br>";
    if($result = mysql_query($query)) return(0);
    else echo(mysql_error());
}
//END db_insert

//////////////////////////////////////////////////
//////////
// Function Name:    db_update()
// Accepts:          varchar table, varchar pk, va
rchar pkval
// Returns:            Success or mysql_error()
//
// Author:            Andrew Deering
// Date:            12/10/04
//
// Purpose:
//
//    This function takes all of the posted form e
lements 
//    and updates $table WHERE $pk = $pkval with n
ew values.
//////////////////////////////////////////////////
/////////
function db_update($table, $pk, $pkval){
    //$query = "UPDATE <table> SET
    $query = "UPDATE ".$table." SET ";
    
    
    //$query = "UPDATE <table> SET $fldnm1 = fldnm
1, $fldnm2 = fldnm2, ... $fldnmN = fldnmN
    //$fldnmN = $fieldnameN //EXPLANATION//
    $trigger = 0;
    foreach($_POST as $field => $value){
    echo $field." = ".$value."<br><br>";
      if($trigger > 0) $query = $query . ", ";
      $query = $query . $field." = trim('$value')"
;
      $trigger++;
    }
    
    
    //$query = "UPDATE <table> SET $fldnm1 = fldnm
1, $fldnm2 = fldnm2, ... $fldnmN = fldnmN WHERE $p
k = $pkval
    $query = $query . " WHERE ".$pk." = ".$pkval;  
  
    
    //IF query runs, return.  else, tell me why
    echo $query."<br><br>";
    if($result = mysql_query($query)) return(0);
    else echo(mysql_error());
}
//END db_update

//////////////////////////////////////////////////
//////////
// Function Name:    db_delete()
// Accepts:          varchar table, varchar pk, va
rchar pkval
// Returns:            Success or mysql_error()
//
// Author:            Andrew Deering
// Date:            12/10/04
//
// Purpose:
//
//    This function deletes FROM $table WHERE $pk 
= $pkval.
//////////////////////////////////////////////////
/////////
function db_delete($table, $pk, $pkval){
    $query = "DELETE FROM ".$table." WHERE ".$pk." 
= ".$pkval;    
    
    //IF query runs, return.  else, tell me why
    //echo $query."<br><br>";
    if($result = mysql_query($query)) return(0);
    else echo(mysql_error());
}
//END db_delete
?>

You might also like