简体   繁体   中英

MySQLi Insert or Update

i am coding a custom loading Screen for my Half-Life Gameserver. I want to display the last Visit, the Map and the Steam Name. Everything works quite well but my last Visit Insert does not work. It worked before and i didn't even changed anything.

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()."')");
    }
}

As you can see, I try to check, if there is an already existing database entry for the steamid. If it is not, it should create one. If it is, it should update the existing one.

But when i run the code, it does not insert anything. (I checked the URL etc.)

Why do you use GET? Those Variables (Map & SteamID64Bit) only works within the URL..

One problem is you are checking if(!empty($map) && !empty($communityid)) inside that you have if(empty($map) . If $map is empty or is not empty the second if statement will never execute. This would work,

$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";

Thanks to sigy i got it to work now. Instead of

// 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()."')");
}

I changed the field 'steamid' to UNIQUE in phpmyadmin and changed this section to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM