PHP - 更新MySQL数据库

时间:2014-02-16 00:24:45

标签: 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';

但是,数据库不会更新。我在Licenses中创建了表license_db。另外,我有一行具有以下值:

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 个答案:

答案 0 :(得分:1)

e.g。

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