note 43884 added to reserved.variables

From: Date: Wed, 07 Jul 2004 09:42:58 +0000
Subject: note 43884 added to reserved.variables
Groups: php.notes 
Request: Send a blank email to php-notes+get-72818@lists.php.net to get a copy of this message
/* this functions get values and keys from $_POST and return a sql string. updates and new release can be founded on www.2bytes.org */ <? function post2sql($excludefields, $table, $sqltype, $where=NULL) /* Author : valk <valk@2bytes.org> * License : GNU/GPL * Version : 0.3 (gushi ghusi) * $excludefields : fields that i wouldnt insert in sql string (ex. submit, hidden input) * $table : table name needed to make sql string * $sqltype : 'edit','add' or 'search' to make a update, insert or select query * $where : where condition to insert in sql string NOTE: it will be appended to the end * of the string * ex. * $strsql=post2sql('Submit, Name','users','add','where id=1'); */ { $i=0; if(!empty($excludefields)) /* this code block create the exclude fields /* array */ $exclude=explode(',',$excludefields); /* :P */ foreach($_POST as $key => $value) /* pre work arrays */ if(exclude_search($exclude,$key)==FALSE) /* if actual key isnt in the exclude array */ switch($sqltype) /* type switch */ { case 'add': /* add type */ $fields[$i]=$key; /* fields[] is the field array */ $values[$i]="'".htmlspecialchars($value)."'"; /* like you suppose values is values array */ $i++; /* pretty $i increment*/ break; case 'edit': /* edit type */ $sets[$i]="$key='".htmlspecialchars($value)."'"; /* no comments :P */ $i++; break; case 'search': /* search type */ if($where) return "(ERR) where field must be NULL."; /* why use where field if u are creating a select query? */ $sets[$i]="($key='".htmlspecialchars($value)."')"; /* no comments :P */ $i++; break; } switch($sqltype) /* making query string */ { /* nothing strange */ case 'add': $strfields=implode(',',$fields); $strvalues=implode(',',$values); $strsql="insert into $table ($strfields) values($strvalues) $where;"; break; case 'edit': $strsets=implode(',',$sets); $strsql="update $table set $strsets $where;"; break; case 'search': $strsets=implode(' and ',$sets); $strsql="select * from $table where $strsets;"; break; } return $strsql; /* return query string */ } function exclude_search($array, $search) /* case insensitive array search */ { $found=FALSE; if(!$array) return $found; foreach ($array as $key => $value) if(strtolower(trim($value))==strtolower(trim($search))) $found=TRUE; return $found; } ?> ---- Manual Page -- http://www.php.net/manual/en/reserved.variables.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+43884 Delete -- http://master.php.net/manage/user-notes.php?action=delete+43884&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+43884&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#72818) next »