简体   繁体   English

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

[英]Use PHP to Update a Value in a MySQLi Database Table

I've written the below function to update a value in a MySQLi database table. 我编写了以下函数来更新MySQLi数据库表中的值。 I'm not receiving any errors, but the value is also not updating. 我没有收到任何错误,但是值也没有更新。 I can't see what's going wrong. 我看不到出了什么问题。

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());
}

I'm calling the function using: 我正在使用以下函数调用该函数:

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!';
}

I've used an echo in the function (see first line) to verify that the function is being called. 我在函数中使用了echo(请参阅第一行)以验证函数是否正在被调用。 This also tells me that $high_score and $user parameters are being passed properly. 这也告诉我$ high_score和$ user参数已正确传递。 I can also replace these variables with actual values, and the function works properly. 我也可以将这些变量替换为实际值,并且该函数可以正常工作。 So at this point, I'm also out of troubleshooting ideas. 因此,在这一点上,我也没有排除故障的想法。 Any help would be much appreciated. 任何帮助将非常感激。

You are using the $user variable twice, and that's rewriting the value. 您使用$user变量两次,并且正在重写该值。 You should rename it. 您应该重命名。

Try with 试试看

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());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM