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

时间:2014-01-01 21:24:30

标签: php mysql mysqli

我编写了以下函数来更新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!';
}

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

1 个答案:

答案 0 :(得分:4)

您正在使用$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());
}