2014-02-16 247 views
0

我試圖用PHP更新MySQL數據庫。PHP - 更新MySQL數據庫

這裏是我的代碼:

$tableName = "Licenses"; 
$searchVariable = "used"; 
$selectVariable = "verCode"; 
$verTime = date('Y-m-d H:i:s'); 
$userUUID = "test_string"; 
$verID = "79A4D"; 

mysql_connect("localhost", "my_user", "my_pass") or die(mysql_error()); 
mysql_select_db("licenses_db") or die(mysql_error()); 
$data = mysql_query("SELECT * FROM `{$tableName}` WHERE `{$searchVariable}`='{$verID}'") or die(mysql_error()); 

while($info = mysql_fetch_array($data)) { 
    //Verification ID unused, so verify the user 
    foreach($info as $key => $value) { 
     echo "$key: $value</br>"; 
     } 
    if ($info['used'] == 0) { 
     echo "<br/>UPDATE `{$tableName}` SET '{$selectVariable}'=1,'time'=`{$verTime}`,'UUID'=`{$userUUID}`; WHERE `{$searchVariable}`='{$verID}'<br/>"; 
      // the above is to see what command is used 
     mysql_query("UPDATE `{$tableName}` SET '{$selectVariable}'=1,'time'=`{$verTime}`,'UUID'=`{$userUUID}`; WHERE `{$searchVariable}`='{$verID}'"); 
     echo "data updated"; 
     return 'Success'; 
     } 
    //Verification ID was used already 
    else { 
     echo "found but used"; 
     return 'Error Message'; 
     } 
    } 
echo "not found"; 
return 'Error Message'; 

但是,數據庫不更新。我在license_db中創建了表Licenses。另外,我有以下值的一行:

verCode = 79A4D 
used = 0 
UUID = NULL 
time = NULL 

如果我第一次運行程序,它應該更新數據庫。這是打印出來:

0: 79A4D 
verCode: 79A4D 
1: 
used: 
2: 
UUID: 
3: 
time: 

UPDATE `Licenses` SET 'used'=1,'time'=`2014-02-15 19:14:13`,'UUID'=`test_string`; WHERE `verCode`='79A4D' 
data updated 

當我運行它的第二次,現在used爲1,所以它應該打印出來found but used。但是,data updated部分(帶有UPDATE ...)已打印出來。

因此,數據庫沒有更新。我該如何解決這個問題?謝謝!

+1

的[MySQL的功能被棄用(http://stackoverflow.com/questions/13944956/the-mysql-extension-is-deprecated-and-will-be-removed-in-the-future- use-mysqli),因此不是開始學習如何從PHP訪問MySQL數據庫的好地方。 –

+0

你注意到你輸入了';'分隔符?嘗試刪除它,看看會發生什麼.. –

+0

@OrelEraki同樣的結果。 –

回答

1

例如

" 
UPDATE `$tableName` 
    SET `$selectVariable` = 1 
    , `time` = '$verTime' 
    , `UUID` = '$userUUID' 
WHERE `$searchVariable` = '$verID'; 
"; 
+0

我有另一個問題。 '$ info ['user']'== 0總是爲真,即使一次user = 1(我使用phpAdmin檢查了數據庫)。如果我嘗試'=='0'',它總是False ... –