2016-08-02 80 views
-4

我不明白爲什麼這不起作用:更新/回聲不工作

$sql = "UPDATE tbl_users SET balance = balance - 250 WHERE userID = <?php echo $row['userID']; ?>"; 

我與PDO

工作,這裏有什麼問題?需要一些幫助

session_start(); 
require_once 'class.user.php'; 
$user_home = new USER(); 

if(!$user_home->is_logged_in()) { 
    $user_home->redirect('index.php'); 
} 

$stmt = $user_home->runQuery("SELECT * FROM tbl_users WHERE userID=:uid"); 
$stmt->execute(array(":uid"=>$_SESSION['userSession'])); 
$row = $stmt->fetch(PDO::FETCH_ASSOC); 

$servername = "test.de.mysql"; 
$username = "test"; 
$password = "test"; 
$dbname = "test"; 


try { 
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); 
    // set the PDO error mode to exception 
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

    $sql = "UPDATE tbl_users SET balance = balance - 250 WHERE userID = <?php echo $row['userID']; ?>"; 

    // Prepare statement 
    $stmt = $conn->prepare($sql); 

    // execute the query 
    $stmt->execute(); 

    // echo a message to say the UPDATE succeeded 
    echo $stmt->rowCount() . " records UPDATED successfully"; 
    } 
catch(PDOException $e) 
    { 
    echo $sql . "<br>" . $e->getMessage(); 
    } 

$conn = null; 
+0

發生任何錯誤? –

+1

嗯....你已經在PHP這裏'<?php echo $ row ['userID']; ?>'應該得到一個關於它的錯誤;一個PARSE錯誤。 –

+0

我不會出錯!你可以在這裏看到它。 kevinshop.de/test.1php –

回答

0
$sql = "UPDATE tbl_users SET balance = balance - 250 WHERE userID = " . intval($row['userID']); 

此外,我會提到你選擇所有userFields,而你只需要用戶ID是不必要的浪費MEM。