Created
March 14, 2011 22:18
-
-
Save niczak/869992 to your computer and use it in GitHub Desktop.
Function/method for getting data (using an associative array map) from a form into a database.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
A simple example including sample function call and array map below. | |
This routine can be easily extended/customized to suit more complex | |
or just individual needs for a given dataset/table. | |
*/ | |
function fnDB_Update($aFields, $aPost, $hDB, $sTable, $bQuery=TRUE) | |
{ | |
$sSQL = "UPDATE $sTable SET drev='now', "; | |
foreach($aFields as $sField=>$sDefault) | |
{ | |
$sSQL .= sprintf("%s%s", | |
$sField."=", | |
!empty($aPost[$sField]) ? "'$aPost[$sField]'," : $sDefault.", "); | |
} | |
$sSQL = rtrim($sSQL, ', '); | |
$sSQL .= sprintf(" WHERE snid = '%s'", $aPost['snID']); | |
// Just for debug | |
// echo "$sSQL<br /><br />\n\n"; | |
// exit(-999); | |
// If user wants to test the function, just pass FALSE (or anything) as | |
// 5th parameter and it will return the SQL string w/o doing the query. | |
if($bQuery === TRUE) | |
{ | |
$hRes = pg_query($hDB, $sSQL); | |
return $hRes; | |
} | |
else | |
return $sSQL; | |
} | |
// Example array map | |
$aMaster = array("sPrefix"=>"''", "sPrefix_Vis"=>"'I,X'", "sFirst"=>"' '", "sLast"=>"' '", | |
"sNick"=>"''", "sNick_Vis"=>"'I,X'", "sMiddle1"=>"''", "sMiddle2"=>"''", "sMiddle3"=>"''", | |
"sMiddle_Vis"=>"'I,X'", "sSuffix"=>"''", "sSuffix_Vis"=>"'I,X'", "sAffil"=>"' '", | |
"sTeachg_Affil"=>"''", "sTeachg_Affil_Vis"=>"'I,X'", "snID"=>"NULL", "snID_Sci"=>"NULL", | |
"sEmail_Alt"=>"''", "sEmail_Alt_Vis"=>"'I,X'", "sKeywords"=>"''", "sScience_Codes"=>"''"); | |
// Example function call | |
$hRes = fnDB_Update($aMaster, $aPost, $hDB, "master", TRUE); | |
$aRes = pg_fetch_all($hRes); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment