无法更新mysql列中的值

时间:2015-05-08 17:10:41

标签: php mysql pdo

我不确定发生了什么。我正在尝试使用暂停用户的函数更新值为0的'users'表。列名为'active',tinyint(1),默认值为0,我尝试使用'active = 0','active = false',active =!active','active = NOT active ',active ='DEFAULT',这些查询都没有返回错误,但没有一个正在更新表...我也试过用多种方式绑定值无效...

function suspendUser($id){

        global $dbc;

        $sus = $dbc->prepare("UPDATE users SET active = 0 WHERE user_id = :id");
        $sus->bindParam(':id', $id, PDO::PARAM_INT);
        $sus->execute();

        if($sus->rowCount() > 0){
            return true;
            }
        else{
            return 'There was an error with your request.';
            }
        }

这是我的配置文件

try { 
$dbc = new PDO("mysql:host=$db_host;dbname=$db_name;charset=utf8",  $db_username, $db_password);
$dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); }
 catch(PDOException $e){ 
echo "Connection failed: " . $e->getMessage(); }

// create the error handler
    function my_error_handler($e_number, $e_message, $e_file, $e_line, $e_vars){
        global $debug, $contact_email;

        $message = "An error occurred in script '$e_file' on line $e_line:     $e_message";
        $message .= print_r($e_vars, 1);

    if($debug){
        echo '<div class="error">'.$message.'</div>';
        debug_print_backtrace();
        }
    else{
        error_log($message, 1, $contact_email);

        if(($e_number != 'E_NOTICE') && ($e_number < 2048)){
            echo '<div class="error">A system error occurred. We apologize for the inconvenience.</div>';
            }
        } 
    }

    set_error_handler('my_error_handler');

这是javascript函数

function suspendUser(uid){
        $.ajax({ url: 'lib/user-edit.php',
                data:{action: 'suspend', id: uid},
                type:'post',
                success: function(t){
                    $('#' + uid).prepend('<p>'+t+'<br>Sucessfully suspended</p>');
                    }
                });
        }

然后

if(isset($_POST['action'])){
        $action = $_POST['action'];
        }
    if(isset($_POST['id'])){
        $id = $_POST['id'];
        }
    if(isset($_POST['role'])){
        $role = $_POST['role'];
        }
    else{
        return 'There was an error with your request.';
        }

// define the function to execute based on the posted action

if($action == 'change'){
    changeRole($id);
    }
if($action == 'delete'){
    deleteUser($id);
    }
if($action == 'suspend'){
    suspendUser($id);
    }

我已将此添加到函数中:

function suspendUser($id){

    global $dbc;

    $sus = $dbc->prepare("UPDATE users SET active = 0 WHERE user_id = :id");
    $sus->bindParam(':id', $id, PDO::PARAM_INT);
    $sus->execute();

     if (!$dbc->execute()) {
        $err = print_r($dbc->errorInfo());
        return $err;
         }

    if($sus->rowCount() > 0){
        $err = print_r($dbc->errorInfo());
        return $err;
        }
    else{
        $err = print_r($dbc->errorInfo());
        return $err;
        }
    }

我仍然没有收到错误。

2 个答案:

答案 0 :(得分:0)

如果要从零更新为零,则行数将为零 - 因为没有任何行发生更改。

答案 1 :(得分:0)

奇怪......通过移动

解决了这个问题
if($action == x)

的陈述
if(asset($_POST['action'])){
  if($action == x){}
}

不确定为什么这是一个问题....我没有得到错误,因为函数没有被调用。其他功能被称为......