gpt4 book ai didi

php - 使用 PHP 更新 MySQLi 数据库表中的值

转载 作者:太空宇宙 更新时间:2023-11-03 10:55:48 25 4
gpt4 key购买 nike

我编写了以下函数来更新 MySQLi 数据库表中的值。我没有收到任何错误,但该值也没有更新。我看不出出了什么问题。

function update_hangman_highscore($user, $user_highscore){
echo 'Update highscore called. High score to update is '.$user_highscore.' for '.$user;
$db = "localhost";
$user = "phpuser";
$pwd = "Ninja1995";
$database = "ninja_comments";
$link = mysqli_connect($db, $user, $pwd)or die(mysqli_connect_error());
mysqli_select_db($link, $database) or die(mysqli_error($link));
$result = mysqli_query($link, "UPDATE users SET hangman_highscore = '$user_highscore' WHERE username = '$user';") or die(mysqli_error());
}

我正在调用该函数:

if($_SESSION['score'] > $_SESSION['user_highscore']){
update_hangman_highscore($_SESSION['user'], $_SESSION['score']);
$_SESSION['message'] = 'Too many wrong guesses. You died, but you also achieved a new personal highscore!';
}

我在函数中使用了回显(见第一行)来验证函数是否被调用。这也告诉我 $high_score 和 $user 参数被正确传递。我也可以用实际值替换这些变量,并且该函数可以正常工作。所以在这一点上,我也没有排除故障的想法。任何帮助将不胜感激。

最佳答案

您正在使用 $user 变量两次,这就是重写值。您应该重命名它。

尝试

function update_hangman_highscore($user, $user_highscore){
echo 'Update highscore called. High score to update is '.$user_highscore.' for '.$user;
$db = "localhost";
$db_user = "phpuser";
$pwd = "Ninja1995";
$database = "ninja_comments";
$link = mysqli_connect($db, $db_user, $pwd)or die(mysqli_connect_error());
mysqli_select_db($link, $database) or die(mysqli_error($link));
$result = mysqli_query($link, "UPDATE users SET hangman_highscore = '$user_highscore' WHERE username = '$user';") or die(mysqli_error());
}

关于php - 使用 PHP 更新 MySQLi 数据库表中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20873432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com