From 5acd24515f566a2bc83f859113279b4c462d6b6f Mon Sep 17 00:00:00 2001 From: Christian Boltz Date: Sun, 4 Nov 2007 00:50:09 +0000 Subject: [PATCH] functions.php: - new function db_update ($table, $where, $values, $timestamp = array()) to update a database entry with the values given as array - added optional $timestamp parameter to db_insert() git-svn-id: https://svn.code.sf.net/p/postfixadmin/code/trunk@184 a1433add-5e2c-0410-b055-b7f2511e0802 --- functions.inc.php | 53 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 10 deletions(-) diff --git a/functions.inc.php b/functions.inc.php index c5c1f8fc..d76b3bb3 100644 --- a/functions.inc.php +++ b/functions.inc.php @@ -675,7 +675,7 @@ function divide_quota ($quota) // // check_owner -// Action: Checks if the admin is the owner of the domain. +// Action: Checks if the admin is the owner of the domain (or global-admin) // Call: check_owner (string admin, string domain) // function check_owner ($username, $domain) @@ -1532,21 +1532,54 @@ function db_delete ($table,$where,$delete) * Call: db_insert (string table, array values) * @param String $table - table name * @param array - key/value map of data to insert into the table. + * @param array (optional) - array of fields to set to now() + * @return int - number of inserted rows */ -function db_insert ($table, $values) +function db_insert ($table, $values, $timestamp = array()) { - $sql_values = "(" . implode(",",escape_string(array_keys($values))).") VALUES ('".implode("','",escape_string($values))."')"; - $table = table_by_key ($table); + foreach(array_keys($values) as $key) { + $values[$key] = "'" . escape_string($values[$key]) . "'"; + } + + foreach($timestamp as $key) { + $values[$key] = "now()"; + } + + $sql_values = "(" . implode(",",escape_string(array_keys($values))).") VALUES (".implode(",",$values).")"; + $table = table_by_key ($table); +echo "*** $sql_values ***"; exit; $result = db_query ("INSERT INTO $table $sql_values"); - if ($result['rows'] >= 1) - { - return $result['rows']; + return $result['rows']; +} + + +/** + * db_update + * Action: Updates a specified table + * Call: db_update (string table, array values, string where) + * @param String $table - table name + * @param String - WHERE condition + * @param array - key/value map of data to insert into the table. + * @param array (optional) - array of fields to set to now() + * @return int - number of updated rows + */ +function db_update ($table, $where, $values, $timestamp = array()) +{ + $table = table_by_key ($table); + + foreach(array_keys($values) as $key) { + $sql_values[$key] = escape_string($key) . "='" . escape_string($values[$key]) . "'"; } - else - { - return true; + + foreach($timestamp as $key) { + $sql_values[$key] = escape_string($key) . "=now()"; } + + $sql="UPDATE $table SET ".implode(",",$sql_values)." WHERE $where"; + + $result = db_query ($sql); + return $result['rows']; }