更新用户组时出错

时间:2015-08-14 17:22:49

标签: php

我尝试在我的网站上实施用户屏蔽功能,但是查询更新存在问题,我无法查看主要原因。&nbsp。错误报告为ON(E_ALL),其他所有工作(注册,登录,查找用户,名称更新),组更新除外。

if (Input::exists()) {
    if (isset($_POST['block-username'])) {
        $banUser = Input::get('ban');
        if(!empty($banUser)) {
            if ($user->find($banUser) == false) {
                echo "That user doesn't exists.";
            } else {
                try {
                    $user->update(array('group' => 4));

                    echo 'User {$user->data()->username} is blocked.';
                } catch (Exception $e) {
                    die($e->getMessage());          
                }                                   
            }
        }
    }
} 

这是更新功能:

/** 
 * Update in database
 *
 * @param string $table
 * @param int    $id
 * @param array  $fields
 * @return bool
 */
public function update($table, $id, array $fields) {
    $set = '';
    $x = 1;
    $countFields = count($fields);

    foreach ($fields as $name => $value) {
        $set .= "{$name} = ?";
        if ($x < $countFields) {
            $set .= ',';
        }
        $x++;
    }

    $sql = "UPDATE {$table} SET {$set} WHERE ID = {$id}";
    if (!$this->query($sql, $fields)->error()) {
        return true;
    }
    return false;
}

但是,更新使用名称:

try {
    $user->update(array('name' => Input::get('name')));

    Session::flash('home', 'Your details have been updated.');
    Redirect::to('index.php');
} catch(Exception $e) {
    die($e->getMessage());  
}

我已检查过与数据库,包含文件和MySQL权限的连接。我只收到消息There was a problem updating

更新:这是我在print_r($e);之前使用die($e->getMessage());获得的内容:

Exception Object ( [message:protected] => There was a problem updating 
                   [string:Exception:private] => 
                          [code:protected] => 0 
                          [file:protected] => /home/nikola/public_html/matehub/classes/User.php 
                          [line:protected] => 52 
                          [trace:Exception:private] => Array ( 
                                [0] => Array ( 
                                       [file] => /home/nikola/public_html/matehub/admin.php 
                                       [line] => 113 
                                       [function] => update 
                                       [class] => User 
                                       [type] => -> 
                                       [args] => Array ( [0] => Array ( [group] => 4 ) 
                                       ) 
                                 )
                          ) 
                          [previous:Exception:private] => )

1 个答案:

答案 0 :(得分:0)

在数据库更新函数中打印sql。

public function update($table, $id, array $fields) {
    $set = '';
    $x = 1;
    $countFields = count($fields);

    foreach ($fields as $name => $value) {
        $set .= "{$name} = ?";
        if ($x < $countFields) {
            $set .= ',';
        }
        $x++;
    }

    $sql = "UPDATE {$table} SET {$set} WHERE ID = {$id}";

    echo $sql; //here

    if (!$this->query($sql, $fields)->error()) {
        return true;
    }
    return false;
}

然后用户将进行调试。

完全按我写的方式尝试这个查询:

UPDATE  `users` SET  `group` =  '4' WHERE  `users`.`id` =32;