如果用户的积分为 10,000,我正在尝试奖励用户徽章。表中有一个名为 badge1 的字段,其默认值设置为locked和一个点行。我正在运行if 语句,如果用户点数为 10,000,则将 badge1 行从锁定更新为解锁。我的代码看起来是正确的,但它既没有更新字段也没有显示任何错误。
<?php
$db = new PDO('mysql:host=hostname;dbname=databasename;charset=UTF-8', 'username', 'password');
$username = $_SESSION['username'];
$q = "SELECT Points FROM login_users WHERE username ='$username'");
$r = mysql_query($q);
$row = mysql_fetch_assoc($r);
$Points = $row['Points'];
if($Points == "10000") {
$awardBadge = $db->exec("UPDATE login_users SET badge1=unlocked WHERE username=?");
$Points->execute(array($username))
} else {
print "";
}
?>
更新:
我设法让它工作..但是问题是我对将旧 sql 转换为 PDO 有点新,所以这不是很安全,但这就是有效的:
<?php
$connect = mysql_connect("host","username","password");
mysql_select_db("databasename");
$username = $_SESSION['jigowatt']['username'];
$q = "SELECT Points FROM login_users WHERE username = ('$username')";
$r = mysql_query($q);
$row = mysql_fetch_assoc($r);
$Points = $row['Points'];
?>
// Place somewhere
<?php
if($Points >= "10000") {
$result = mysql_query("UPDATE login_users SET maneki='unlocked' WHERE username='$username'");
} else {
print "Badge has not been unlocked";
}
?>