0

我正在创建 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();
4

3 回答 3

0

这是我会怎么做的

$time = _SESSION[test];
$user = "john";

$res = $con->prepare("SELECT time from eurokos WHERE userName = '$user' AND time > '$time'");
$res->execute();
$allow_new_insert = $res->num_rows;

 if(!$allow_new_insert) {
  //insert magic code for updating time here
 }
于 2013-05-11T16:36:13.800 回答
0

在插入记录之前执行此操作

  1. 运行选择查询并查看该用户名是否存在。

  2. 如果存在,则比较两个时间,如果 db 时间大于要插入的值,则运行更新查询将时间更新为新值。

  3. 如果它不存在,则为该用户运行插入查询。

于 2013-05-11T16:16:08.703 回答
0

仅更改您的选择:

SELECT userName,min(time) time FROM eurokos group by userName order by time ASC LIMIT 10;
于 2013-05-11T16:19:54.360 回答