簡體   English   中英

使用PHP更新MySQLi數據庫表中的值

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

我編寫了以下函數來更新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參數已正確傳遞。 我也可以將這些變量替換為實際值,並且該函數可以正常工作。 因此,在這一點上,我也沒有排除故障的想法。 任何幫助將非常感激。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM