I have a database keeping track of game records coming in via JSON. If an ID does not exist I simple want to create a new record but if it does exist I want to update the values. So if a player had 10 kills previously and then they played another game and added 10 more kills I would want their total to update to 20 kills based of their ID. Is my code the best way to do it?
<?php
$con = mysqli_connect("localhost", "root", "","playerstats") or die('Could not connect: ' . mysqli_error());
error_log("You messed up!", 3, "my-errors.log");
//$raw_data = file_get_contents('php://input');
$raw_data = file_get_contents('test.json');
$data = json_decode($raw_data, true);
$SteamID = $data['SteamID'];
$Username = $data['Username'];
$Kills = $data['Kills'];
$Deaths = $data['Deaths'];
$Rank = $data['Rank'];
$sql = "INSERT INTO playerinfo(SteamID, Username, Kills, Deaths, Rank)
VALUES('$SteamID', '$Username', '$Kills', '$Deaths', '$Rank')
ON DUPLICATE KEY UPDATE Kills += $Kills, Deaths += $Deaths";
if(!mysqli_query($con,$sql))
{
die('Error : ' . mysqli_error($con));
}
?>