Ejemplo n.º 1
0
function _s_db_update($table, &$v1, &$v2)
{
    if (s_bad_string($table) || s_bad_array($v1) || s_bad_array($v2) || s_bad_id($v1["id"], $pid)) {
        return s_err_arg("no primary key.");
    }
    if (defined("APP_DB_PREFIX")) {
        //替换表名:"%s_user:update" => "201204disney_user:update"
        $table = sprintf($table, APP_DB_PREFIX, true);
    }
    if (isset($v2["id"])) {
        //防止更新主键
        unset($v2["id"]);
    }
    // 防止有重复的值
    $v2 = array_unique($v2);
    // 对$v1和$v2数据归类
    $values = array();
    foreach ($v2 as $key => $value) {
        if ($v1[$key] == $v2[$key]) {
            continue;
        }
        $values[] = "`{$key}`=" . (is_string($value) ? '"' . s_safe_value($value) . '"' : $value);
    }
    $sql = "update `{$table}` set " . implode(", ", $values) . " where `id`={$pid}";
    return s_db_exec($sql);
}
Ejemplo n.º 2
0
function _s_db_update($table, &$v1, &$v2)
{
    if (s_bad_string($table) || s_bad_array($v1) || s_bad_array($v2) || s_bad_id($v1['id'], $pid)) {
        //没有指定主键,更新失败
        return s_log("no primary key.");
    }
    if (defined("APP_DB_PREFIX")) {
        //替换表名:"%s_user:update" => "201204disney_user:update"
        $table = sprintf($table, APP_DB_PREFIX, true);
    }
    if (isset($v2["id"])) {
        //防止更新主键
        unset($v2["id"]);
    }
    // 防止有重复的值
    $v2 = array_unique($v2);
    // 对$v1和$v2数据归类
    $values = array();
    foreach ($v2 as $key => $value) {
        if (!isset($v1[$key]) || $v1[$key] != $v2[$key]) {
            $values[] = "`{$key}`=" . (is_string($value) ? '"' . s_safe_value($value) . '"' : $value);
        }
    }
    if (empty($values)) {
        //不需要修改
        return false;
    }
    return s_db_exec("update `{$table}` set " . implode(", ", $values) . " where `id`={$pid}");
}