MySQLi插入或更新

时间:2014-02-04 16:38:37

标签: php mysqli sql-update sql-insert

我正在为我的半条命游戏服务器编写一个自定义加载屏幕。 我想显示上次访问,地图和Steam名称。 一切都很好,但我上次访问插入不起作用。 它以前工作过,我甚至没有改变任何东西。

include('mysql.php');

$map = $_GET['map'];
$communityid = $_GET['steamid'];
$apikey = "***SECRETAPIKEY***";

if(!empty($map) && !empty($communityid)) {

    if(empty($map)) { $map = "undefined"; }

    $authserver = bcsub( $communityid, '76561197960265728' ) & 1;
    $authid = ( bcsub( $communityid, '76561197960265728' ) - $authserver ) / 2;

    $steamid = "STEAM_0:$authserver:$authid";

    // Load Player data
    $xml = simplexml_load_file('http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key='.$apikey.'&steamids='.$communityid.'&format=xml');
    $name = $xml->players->player->personaname;
    $avatar = $xml->players->player->avatarfull;

    // Fetch Last online from User
    $lastonline_get = mysqli_query($db, "SELECT * FROM lastonline WHERE steamid = '".$steamid."'");
    while($row = mysqli_fetch_object($lastonline_get))
    {
      $user_lastonline = $row->lastonline;
    }

    // Count from last connect
    function diff_time($differenz)
    {
        $differenz = time() - $differenz;
        $tag  = floor($differenz / (3600*24)); 
        $std  = floor($differenz / 3600 % 24); 
        $min  = floor($differenz / 60 % 60); 
        $sek  = floor($differenz % 60); 

        return array("sek"=>$sek,"min"=>$min,"std"=>$std,"tag"=>$tag,"woche"=>$woche); 
    }

    $difftime_lastonline = diff_time($user_lastonline);

    if($difftime_lastonline['tag']!=0) { $user_whenlastonline = $difftime_lastonline['tag']." Tage ".$difftime_lastonline['std']." Stunden und ".$difftime_lastonline['min']." Minuten "; }
    elseif($difftime_lastonline['std']!=0) { $user_whenlastonline = $difftime_lastonline['std']." Stunden und ".$difftime_lastonline['min']." Minuten "; }
    else { $user_whenlastonline = $difftime_lastonline['min']." Minuten "; }

    // Last online Updaten

    if(mysqli_query($db, "SELECT * FROM lastonline WHERE steamid = '".$steamid."'")) {
        $lastonline_updaten = mysqli_query($db, "UPDATE lastonline Set lastonline = '".time()."' WHERE steamid = '".$steamid."'");
    } else {
        $lastonline_eintragen = mysqli_query($db, "INSERT INTO lastonline (steamid, lastonline) VALUES ('".$steamid."', '".time()."')");
    }
}

如您所见,我尝试检查是否存在已存在的steamid数据库条目。如果不是,它应该创建一个。如果是,它应该更新现有的。

但是当我运行代码时,它不会插入任何内容。 (我检查了URL等。)

为什么要使用GET? 那些变量(Map& SteamID64Bit)仅适用于URL ..

2 个答案:

答案 0 :(得分:0)

一个问题是你在if(!empty($map) && !empty($communityid))内检查if(empty($map)。如果$ map为空或不为空,则第二个if语句将永远不会执行。这样可行,

$communityid = $_GET['steamid'];
$apikey = "***SECRETAPIKEY***";

if(empty($_GET['map']))
{ 

    $map = 'undefined map'

}
else if (!empty($_GET['map']) && !empty($communityid)
{

    $map = $_GET['map'];

    $authserver = bcsub( $communityid, '76561197960265728' ) & 1;
    $authid = ( bcsub( $communityid, '76561197960265728' ) - $authserver ) / 2;

    $steamid = "STEAM_0:$authserver:$authid";

答案 1 :(得分:0)

感谢sigy,我现在就开始工作了。 而不是

// Last online Updaten

if(mysqli_query($db, "SELECT * FROM lastonline WHERE steamid = '".$steamid."'")) {
    $lastonline_updaten = mysqli_query($db, "UPDATE lastonline Set lastonline = '".time()."' WHERE steamid = '".$steamid."'");
} else {
    $lastonline_eintragen = mysqli_query($db, "INSERT INTO lastonline (steamid, lastonline) VALUES ('".$steamid."', '".time()."')");
}

我在phpmyadmin中将字段'steamid'更改为UNIQUE,并将此部分更改为

    // Last online Updaten
    $lastonline_eintragen = mysqli_query($db, "INSERT INTO lastonline (steamid, lastonline) VALUES ('".$steamid."', '".time()."') ON DUPLICATE KEY UPDATE lastonline='".time()."'");