2015-05-08 55 views
1

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

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; 
     } 
    } 

我仍然没有得到错误。

+1

获取* actual *错误,让我们知道那是什么。像我说的那样, –

+0

....没有错误。一切行为都像查询成功一样。 – MAtkins

+1

不,有错误,你只是不报告他们。 [PDO错误检查](http://php.net/manual/en/pdo.error-handling.php) –

回答

0

如果您从零更新为零,行计数将为零 - 因为没有任何行更改。

+0

来自[docs](http://php.net/manual/en/pdostatement.rowcount.php)*“rowCount()返回受相应执行的最后一个DELETE,INSERT或UPDATE语句影响的行数PDOStatement对象“。* –

+0

我试图将值从1更改为0. db中的值当前为1。 – MAtkins

0

奇怪的是......问题是由

if($action == x) 

语句移动到

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

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