Added the update method for easier updates w/o SQL.

git-svn-id: http://svn.cleancode.org/svn/pickles@21 4d10bc64-7434-11dc-a737-d2d0f8310089
This commit is contained in:
Josh Sherman 2008-06-19 01:01:43 +00:00
parent 78ff970b15
commit 8888c6a2a6

View file

@ -172,7 +172,49 @@ class DB {
} }
public static function update($table, $columnValues, $conditions) { public static function update($table, $columnValues, $conditions) {
if (!is_resource(self::$connection)) {
self::open();
}
if (trim($table) != '') {
// @todo Check that the table exists, and possibly check that the columns exist as well
$fields = $where = null;
if (is_array($columnValues)) {
foreach ($columnValues as $key => $value) {
$fields .= ($fields ? ', ' : null) . $key . " = '" . mysql_real_escape_string(stripslashes($value), self::$connection) . "'";
}
if (is_array($conditions)) {
foreach ($conditions as $key => $value) {
$where = ($where == null) ? 'WHERE ' : ' AND ';
if ($value == null) {
$where .= $key . ' IS NULL';
}
else {
$where .= $key . " = '" . mysql_real_escape_string(stripslashes($value), self::$connection) . "'";
}
}
$sql = 'UPDATE ' . $table . ' SET ' . $fields . $where;
if (self::execute($sql)) {
return true;
}
}
else {
Error::addError('No conditions were specified');
}
}
else {
Error::addError('No data was specified');
}
}
else {
Error::addError('No database table was specified');
}
return false;
} }
public static function delete($table, $columnValues, $conditions) { public static function delete($table, $columnValues, $conditions) {