2012-07-26 63 views
0

我有下面的代碼,它在更新分數和日期時工作正常。但它不會更新該行的名稱或國家。這是否與PHP字符串有關?很困惑!UPDATE表mysql&php

$userName = "John"; 
$userCountry = "USA"; 
$lowestScoreId = 99; 
$userPoints = 500; 


include 'config.php'; 


$currentTime = time(); 

mysql_query("UPDATE highScores SET name = $userName WHERE id='$lowestScoreId'"); 
mysql_query("UPDATE highScores SET score = $userPoints WHERE id='$lowestScoreId'"); 
mysql_query("UPDATE highScores SET country =$userCountry WHERE id='$lowestScoreId'"); 
mysql_query("UPDATE highScores SET date = $currentTime WHERE id='$lowestScoreId'"); 
+0

MySQL表格字段設置是否正確? varchar ? – 2012-07-26 19:18:53

回答

11

您忘記了設置值的引號。你可以在1個查詢中做到這一點。

UPDATE highScores 
SET `name` = '$userName', 
    `score` = '$userPoints', 
    `country` = '$userCountry', 
    `date` = '$currentTime' 
WHERE id='$lowestScoreId'" 
1

您應該在一個語句中這樣做。

$userName = "John"; 
$userCountry = "USA"; 
$lowestScoreId = 99; 
$userPoints = 500; 

include 'config.php'; 

$currentTime = time(); 

mysql_query("UPDATE highScores SET name = '$userName', score = '$userPoints', country = '$userCountry', date = '$currentTime' WHERE id='$lowestScoreId'"); 
1

此外,你不應該再使用PHP的mysql_函數。看看MySQLi這是更新,更快,並有更多的功能。

+0

或者,就此而言,您可以使用PDO http://php.net/manual/en/book.pdo.php - 它與MySQLi類似,但具有支持MySQL以外的數據庫的優勢 – 2012-07-26 19:36:56