2012-07-26 43 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