【问题标题】:Error with updating user group更新用户组时出错
【发布时间】:2015-08-14 17:22:49
【问题描述】:

我正在尝试在我的网站上实现用户阻止功能,但查询更新出现问题,我看不出主要原因是什么。错误报告为 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

更新:这是我在die($e-&gt;getMessage()); 之前使用print_r($e); 得到的:

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] => )

【问题讨论】:

    标签: php


    【解决方案1】:

    在你的数据库更新函数中打印 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;
    

    【讨论】:

    猜你喜欢
    • 2016-12-17
    • 1970-01-01
    • 1970-01-01
    • 2015-12-21
    • 2021-05-06
    • 1970-01-01
    • 2018-02-25
    • 2017-05-29
    • 2020-11-10
    相关资源
    最近更新 更多