0

I´m trying to save data from a unity game in an sql database, to then be displayed on a website. I´ve found free hosting for both the database and website. So far the website works fine and is able to connect to the db and display already existing data. Now i want to connect the unity app to the db so i can save a player´s name and their highscore. I´m using a c# script to send data to a php script through UnityWebRequest (this is the proper way as far as I know), and from there using POST and a query to check for existing entries and adding new ones/updating old ones.

//Data provided by user
$user = $_POST["user"];
$points = $_POST["points"];

//Query
$sql = "SELECT * FROM scores WHERE name = " . $user;
//$sql2 = 
//$sql3 = 
$result = $db->query($sql);
//$result2 = $db->query($sql2);
//$result3 = $db->query($sql3);

//If name exists, change existing points, else new entry
if ($result->num_rows > 0) {
    "UPDATE scores SET score = . $points WHERE score < . $points AND name = " . $user;
} else {
    "INSERT INTO scores VALUES (. $user, . $points)";
}

The main question is, does the php work for what I´m trying to do and is the way I´m doing this ok or should I do something differently? Also how do you recommend I host the php file?

Edit: So far, I´m returning a 400 Bad Request in my unity console

ziky2210
  • 1
  • 1
  • If you had tried it, you would know that no, it doesn't. You can use a local server for testing, but if this is something that needs to be world-accessible, you'll need a web hosting server. – aynber Jun 29 '22 at 13:36
  • 2 things... 1) Check the Apache error log and/or the php error log - that will help and hopefully tell you why its a Bad Request and 2) You can test this by using a tool like postman to make the post calls while developing the php bit. I will also point out the PHP here is very much open to abuse, anyone can post to the 'endpoint' and as already mentioned SQL Injection issues. – Brian Jun 29 '22 at 14:09

0 Answers0