我一直在徒勞地尋找這個問題的答案,也許是因為搜索詞不是用幾句話就能最好地表達的。
我有一張表來存盤玩我創建的游戲的人的姓名和分數。即使名稱已經存在,我也希望每次都允許一個新的條目實體。例如,前 10 名的分數可能都來自玩家 John,并且所有分數都可能不同。
約翰 - 10
約翰 - 9
約翰 - 8 等等
但是,當前發生的情況是,當 John 得分 11 時,所有名為 John 的條目都會更新為 11。
我使用的陳述句是:
<?php
if(isset($_GET['username']))
{
//Include the connection to the database
include("connection.php");
$link = getDBConnection();
//Define the local variables
$username = $_GET['username'];
$score = $_GET['score'];
$query2 = "INSERT INTO `scoreboard` (`ID`, `username`, `score`) VALUES (NULL, '".mysqli_real_escape_string($link, $username)."', '".$score."');";
$result = mysqli_query($link, $query2);
}else{
echo "Error: Username has not been recieved in global POST array";
}
?>
請問有人可以幫我嗎?
uj5u.com熱心網友回復:
如圖所示,我已在 dbFiddle 上對情況進行了編碼,并且無法重現該錯誤。
首先要檢查的是表定義。當您插入 null 作為 ID 時,我假設您有一個 auto_increment 列。(不指定列比使用 null 指定更簡單)。
注意您的代碼存在 SQL 注入的風險,因為您使用分數而不檢查任何內容。以下行將接受任何有效數字,如果該值不是數字,則會停止并出現錯誤。
$score = 1 * $_GET['score'];
create table scoreboard ( id int not null primary key auto_increment, username varchar(25), score int);?
insert into scoreboard ( username,score) values ('John',8),('John',9),('John',10);?
insert into scoreboard (id, username,score) values (null,'John',11);?
select * from scoreboard;編號 | 用戶名 | 分數 -: | :------- | ----: 1 | 約翰 | 8 2 | 約翰 | 9 3 | 約翰 | 10 4 | 約翰 | 11
db<>在這里擺弄
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/461708.html
上一篇:具有精確值的MySQL觸發器
下一篇:顯示按特定列分隔的所有列
