PHP-更新 MySQL 数据库

标签: MySQL PHP
发布时间: 2015/6/11 7:16:23
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我试图用 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:

例如

"
UPDATE `$tableName`  
   SET `$selectVariable` = 1
     , `time` = '$verTime'
     , `UUID` = '$userUUID'
 WHERE `$searchVariable` = '$verID';
";
官方微信
官方QQ群
31647020