/*--------------------------------------------------------
* Function: sql_update($table, $data, $where='1')
* Creates an SQL query for updating data in a table
*
* Parameters:
* $table -
* $data -
* $where -
*
* Returns:
* Escaped SQL query ready to de executed
*
* Usage:
* $table = DB_TABLE;
* $data['uppdaterad']=$datum;
* $where = "id=$myid";
* $sql = sql_update($table, $data, $where);
* $result = mysql_query($sql);
* if (!$result) { print(mysql_error()); }
*------------------------------------------------------ */
function sql_update($table, $data, $where='1') {
$q="UPDATE `".$table."` SET ";
foreach($data as $key=>$val) {
if(strtolower($val)=='null') $q.= "`$key`=NULL, ";
elseif(strtolower($val)=='now()') $q.= "`$key`=NOW(), ";
elseif (is_numeric($value)) $q.= "`$key`=".intval($value).", ";
else $q.= "`$key`='".escape($val)."', ";
}
# Ta bort sista ", "
$q = rtrim($q, ', ') . ' WHERE '.$where.';';
return $q;
}
0 comments:
Post a Comment