-4
我有一個名爲'Users'的表的SQL數據庫,其中包括:id,userName,password,firstName,lastName和playerScore。問題:使用PHP在mysql表中更新值使用PHP
我試圖讓我的程序更新玩家的分數,如果他們登錄並贏得遊戲。所以從我下面提供的代碼,如果他們登錄,我抓住他們的用戶名和他們當前的分數。如果他們贏了我增加他們的分數並更新它,我遇到的問題是我不確定如果我使用正確的語句來更新它,我嘗試了兩個(已被註釋掉)UPDATE和INSERT更新我的表格爲新的分數,但我的數據庫沒有任何變化。
關於如何進行的任何想法?
$query = "SELECT * FROM Users WHERE userName='$userName' and playerscore='$playerScore'";
$result = mysqli_query($mysql_handle, $query);
if(mysqli_num_rows($result) == 1) {
$row = mysqli_fetch_array($result);
$_SESSION['playerScore'] = $row['playerScore'];
$_SESSION['valid_user'] = $row['userName'];
}
echo "<br>You Win!<br>";
$newScore = $_SESSION['playerScore']++;
//$sql = "INSERT INTO Users (playerScore) VALUES ('$newScore')";
//$sql = "UPDATE Users SET playerScore = $_SESSION['playerScore'] WHERE userName="$_SESSION['valid_user']";
對不起,更新用戶聲明應該是playerScore ='$ newScore',不知道這是否會有所作爲。 – tsab
有一個playerscore字段名稱在查詢和$ _SESSION ['playerScore'] = $ row ['playerScore'];並使用更新語句。 –
update query >> $ sql =「UPDATE Users SET playerScore ='」。$ _ SESSION ['playerScore']。「'WHERE userName ='$ _ SESSION [valid_user]'」; – CodeLove