note 43884 deleted from reserved.variables by aidan
| From: | aidan@php.net | Date: | Mon, 13 Sep 2004 07:37:39 +0000 |
| Subject: | note 43884 deleted from reserved.variables by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-76586@lists.php.net to get a copy of this message | ||
Note Submitter: valk <valk at 2bytes dot org>
----
/*
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;
}
?>