2016-11-24 55 views
-1

對於你的天才來說,這可能是一個非常簡單的問題。我正在嘗試使用php代碼來檢查我的數據庫,以查看用戶是否有足夠的分數。然後如果他們有足夠的積分,那麼從那裏扣除積分。 這是我目前使用不工作:使用php/mysql檢查和扣分

<?php 
    $con = mysqli_connect("localhost", "id177667_root", "***", "id177667_loginb"); 
    $response = array(); 
    $statement = mysqli_prepare($con, "SELECT * FROM user WHERE username = ?"); 
    mysqli_stmt_bind_param($statement, "s", $_POST["username"]); 
    mysqli_stmt_execute($statement); 

    mysqli_stmt_store_result($statement); 
    mysqli_stmt_bind_result($statement, $userID, $name, $username, $password, $points); 
    $pointsint = (int)$points; 

    if ($pointsint > 12500){ 
     $statement = mysqli_prepare($con, "UPDATE user SET points = points - ? WHERE username = ?"); 
     mysqli_stmt_bind_param($statement, "is", 12500, $_POST["username"]); 
     mysqli_stmt_execute($statement); 

     mysqli_stmt_store_result($statement); 
     mysqli_stmt_bind_result($statement, $userID, $name, $username, $password, $points); 

     while(mysqli_stmt_fetch($statement)){ 
      $response["success"] = true; 
      $response["name"] = $name; 
      $response["username"] = $username; 
     } 
    }else{ 
     $response["success"] = false; 
    } 

    echo json_encode($response); 
?> 
+1

沒有足夠的時間,現在看你的問題,但作爲建議:不要選擇用戶的所有數據。沒有必要選擇*從...爲什麼不簡單地「選擇點從用戶」? Rella不需要加載密碼和所有的東西。 – Twinfriends

回答

0

你也許可以做到這一點在SQL沒有像第一SELECT命令:

UPDATE `user` SET `points` = (
    CASE 
     WHEN `points` > 12500 THEN `points` = `points` - ? 
     ELSE `points`=`points` 
    END 
) WHERE username = ?