在我的数据库中,我的用户每个人都有自己的" gold",我已经制作了一个按钮,每次点击它就会增加100金币。问题是,我无法弄清楚如何为特定登录用户执行此操作。这是我当前的更新代码
$addgold2 = mysqli_query($con,"UPDATE stats SET stats.gold = stats.gold + 100 WHERE stats.id=$retval2");
如果我删除" $ retval2"并只输入ID,它的工作原理。我想为登录用户调用ID,然后只更新这些统计信息。这是我的代码
$id2 = "SELECT users.id FROM users WHERE users.username=$username";
$retval2 = mysqli_query($con,$id2);
现在,如果我回显$ id2,它会显示ID为" 1",但它不是1!它的7 :(
我也收到此错误
Catchable fatal error: Object of class mysqli_result could not be converted to string in C:\wamp\www\Game\addgold.php on line 19
在我输入的第一行代码
最后,这里是整个代码(表单在不同的页面上)
<?php error_reporting(E_ALL); ini_set('display_errors', 1);
include("connect.php");
include("header.php");
$username = $_SESSION['userlogin'];
$id2 = "SELECT users.id FROM users WHERE users.username=$username";
$retval2 = mysqli_query($con,$id2);
$sql = "SELECT stats.id, stats.gold, users.id, users.username FROM stats, users WHERE users.username = '$username' AND stats.id = users.id";
$retval = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($retval, MYSQL_ASSOC))
{
echo "Gold: {$row['gold']} <br> ".
"--------------------------------<br>";
}
$row2 = mysqli_fetch_row($retval2);
$id2 = mysqli_num_rows($retval2) or die(mysql_error()." ".$id2);
echo $id2;
if(isset($_SESSION['userlogin'])){
$addgold2 = mysqli_query($con,"UPDATE stats SET stats.gold = stats.gold + 100 WHERE stats.id=$retval2");
if(isset($addgold2['submit'])){
}
echo "You have earned 100 gold!";
mysqli_close($con);
}
?>
答案 0 :(得分:1)
你错过了$id2
sql
$id2 = "SELECT users.id FROM users WHERE users.username='$username'";
答案 1 :(得分:1)
$id2 = mysqli_num_rows($retval2) or die(mysql_error()." ".$id2);
如果它有1行而不是ID
,它将返回1$addgold2 = mysqli_query($con,"UPDATE stats SET stats.gold = stats.gold + 100 WHERE stats.id=$retval2");
$ retval2是mysqli_query结果,那不是ID 我认为$ row2 [&#34; id&#34;]是你的身份。