PHP/Mysql/Highscore表-如何检查现有分数并替换它们
我有一个简单的游戏和高分系统。用户可以写下他的名字,他的分数将保存在highscore表中。问题是:当一个用户用同一个名字玩几次游戏时,数据库中总是有一个新条目。如何更改代码,使每个名称只有一个条目。我如何检查现有的用户名分数,并且只在更好的时候更新分数?如何在我的代码中实现这一点?提前谢谢PHP/Mysql/Highscore表-如何检查现有分数并替换它们,php,mysql,database,sql-update,Php,Mysql,Database,Sql Update,我有一个简单的游戏和高分系统。用户可以写下他的名字,他的分数将保存在highscore表中。问题是:当一个用户用同一个名字玩几次游戏时,数据库中总是有一个新条目。如何更改代码,使每个名称只有一个条目。我如何检查现有的用户名分数,并且只在更好的时候更新分数?如何在我的代码中实现这一点?提前谢谢 <?php header('Access-Control-Allow-Origin: *'); require 'config.php'; mysql_connect("$host", "$use
<?php
header('Access-Control-Allow-Origin: *');
require 'config.php';
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
if(isset($_GET['name']) && isset($_GET['score'])) {
//Lightly sanitize the GET's to prevent SQL injections and possible XSS attacks
$name = strip_tags(mysql_real_escape_string($_GET['name']));
$score = strip_tags(mysql_real_escape_string($_GET['score']));
$sql = mysql_query("INSERT INTO `$tbl_name` (`id`,`name`,`score`) VALUES ('','$name','$score');");
if($sql){
//The query returned true - now do whatever you like here.
echo 'Your score was saved. Congrats!';
}else{
//The query returned false - you might want to put some sort of error reporting here. Even logging the error to a text file is fine.
echo 'There was a problem saving your score. Please try again later.'.mysql_error();;
}
}else{
echo 'Your name or score wasnt passed in the request. Make sure you add ?name=NAME_HERE&score=1337 to the tags.';
}
mysql_close($dblink);//Close off the MySQL connection to save resources.
?>
您可以这样尝试:
使您的姓名列唯一:
CREATE TABLE highscore (
name VARCHAR(255) NOT NULL UNIQUE,
id INT NOT NULL UNIQUE,
score INT NOT NULL DEFAULT 0
PRIMARY KEY (username, id)
);
如果用户存在,则check的分数越高。然后使用新的highscore进行更新
INSERT INTO
`$tbl_name`
SET
`name` = '$name',
`score` = '$score'
ON DUPLICATE KEY UPDATE
`score` = IF (VALUES(`score`) > `score`, VALUES(`score`), `score`) ;
这是否检查现有分数,并仅在新分数更好时更新?对不起,我忘记检查分数了。等一下。好的,你现在可以测试了。很好,谢谢。你知道如何将大写的“姓名”写入数据库吗?好极了,你需要通过对方的姓名来告诉对方他们有回复。我认为@hansistr没有收到你的留言,因为他/她把同样的问题重新问了一遍。
if(isset($_GET['name']) && isset($_GET['score'])) {
$name = strip_tags(mysql_real_escape_string($_GET['name']));
$score = strip_tags(mysql_real_escape_string($_GET['score']));
$checkExist = mysql_query("SELECT `name`, `score` FROM `$tbl_name` WHERE `name` = '$name'");
$row = mysql_fetch_assoc($checkExist);
if (mysql_num_rows($checkExist) > 0){
if ($score > $row['score']){
$sql = mysql_query("UPDATE `$tbl_name` SET `score` = '$score' WHERE `name` = '$name'");
} else {
// ERROR MSG: Your new score is lower.(not updating the database)
}
} else {
$sql = mysql_query("INSERT INTO `$tbl_name` (`id`,`name`,`score`) VALUES ('','$name','$score');");
}
if ($sql){
//The query returned true - now do whatever you like here.
echo 'Your score was saved. Congrats!';
} else {
//The query returned false - you might want to put some sort of error reporting here. Even logging the error to a text file is fine.
echo 'There was a problem saving your score. Please try again later.'.mysql_error();;
}