我正在创建 Flash 游戏,玩家需要尽快通过游戏。我已经将玩家排在前 10 名,并且按时间排序(从最低到最高),但我有一个问题,如果同一个玩家玩了几次,他总是添加到数据库中。我需要让那个球员和他的最佳(最低)时间只被写入数据库一次,或者只打印一次他的名字。(如果玩家的时间低于保存在数据库中的时间,那么我需要更新/插入它。)谢谢。
示例如何:
Peter 00:07
Peter 00:09
Peter 00:12
John 00:17
John 00:21
示例应该如何:
Peter 00:07
John 00:17
这是我的 SelectFromDB.php
<?php
$time = $_POST['time'];
session_start();
$name = $_SESSION['vardas'];
$time = strtotime($time);
$times = date('s:H:i', $time);
$_SESSION['test'] = $times;
$_SESSION['test1'] = $username;
$mysqli = new mysqli("localhost","my_db","pass","my_db");
$query = "SELECT userName,time FROM eurokos ORDER by time ASC LIMIT 10";
if (!$mysqli->set_charset("utf8")) {
printf("Error loading character set utf8: %s\n", $mysqli->error);
} else {
// printf(" ", $mysqli->character_set_name());
}
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<results>';
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_assoc()) {
echo $row["userName"].' '.$row["time"]."\n";
}
$result->free();
}
echo '</results>';
$mysqli->close();
?>
这是我的 InsertToDB.php
<?php
$time = $_POST['time'];
session_start();
$name = $_SESSION['vardas'];
$time = strtotime($time);
$times = date('s:H:i', $time);
$_SESSION['test'] = $times;
$_SESSION['test1'] = $username;
$mysqli = new mysqli("localhost","my_db","pass","my_db");
if ($stmt = $mysqli->prepare("INSERT into eurokos (time, userName) VALUE (?,?) ")) {
if (!$mysqli->set_charset("utf8")) {
printf("Error loading character set utf8: %s\n", $mysqli->error);
} else {
// printf(" ", $mysqli->character_set_name());
}
$stmt->bind_param('ss', $times, $name);
// $stmt->bind_param("s", $username);
$stmt->execute();
if ($stmt->error != '') {
echo ' error:'.$stmt->error;
} else {
echo 'success';
}
$stmt->close();
} else {
echo 'error:'.$mysqli->error;
}
$mysqli->close();