Save Variables from HTML5 to MySQL or Text or Emailed

Hi,

I’m trying to save some variables from a HTML5 game. I’d be happy if I could get them saved in any format. Like in a MySQL database, a Text File, or Emailed.

I’ve been following this tutorial GDevelop 5 [GDevelop wiki] to save them to a mysql database.
(But I would be happy them being saved to a txt file or even emailed.)

This is the code of the page Gdevelop is sending its network request to, I used from that tutorial.

In Gdevelop in the “Request Body Content” I have
“&gname=”+GlobalVariableString(name)+“&gscore=”+GlobalVariableString(score)+“&gpoints=”+GlobalVariableString(points)

I’ve tried tweaking it a bit, with no success.

Any help to save variables to a MySQL Database, Text File, or Email would be appreciated.

Hello,
you need a server for that, free hosting with a mysql database should suffice.

In Gdevelop5 you give “Send a request to web page”

  • Host: “(http or https): //your_domain.com/”
  • Path: directory on the server, basically just this “”
  • Request body content:“YOUR_VAR=data” or
  • Method: “POST” is safer than GET
  • Content type: for JSON “application / json; charset = utf-8”, but for my example set “”
  • Response…var: your_var

On the server side:
create: index.php
MySQL: create a table and user

To index.php:
(for example)

<?php
header('Content-type:application/json;charset=utf-8');

if($_SERVER["REQUEST_METHOD"] == "POST"){
    $data = $_POST['YOUR_VAR'];
    
    if(!empty($data)){
        echo json_encode($data);
    }else{
        //echo "NOT EXIST DATA";
    }
}else{
    //echo "NO POST";
}

if($_SERVER["REQUEST_METHOD"] == "GET"){
    $data = $_GET['YOUR_VAR'];
    
    if(!empty($data)){
        echo json_encode($data);
    }else{
        //echo "NOT EXIST DATA";
    }
}else{
    //echo "NO GET";
}

?>

tested and works
Have a nice rest of the day

Thank you for your help Snyrch, I really appreciate it.

I entered what you said in Gdevelop.

I made a php page and inserted the code you said, replacing YOUR_VAR with GlobalVariableString(score). (Is that correct?)

Do you know how I get that php page to insert the variable into my MySQL database?
I tried adding this code into the php page.

$link = mysql_connect(‘localhost’, ‘my-database-username’, ‘Password’);
if (!$link) {
die('Database server unavalaible ’ . mysql_error());
}

$db = mysql_select_db(‘database-name’, $link);
if (!$db) {
die ('GDProject database unavalaible : ’ . mysql_error());
}

$sql = “INSERT INTO player (score)
VALUES(’$_POST[data]’)”;

It does connect to the database. As every time I click the button in my HTML5 game to send the data, it creates a new row in the database, however all the rows are blank.

Do you know what I have to have on that index.php page to send the “GlobalVariableString(score)” to the database?

Thank you.

Tested and works,
Database:
image
Code: return “OK” suscesful or “ERROR”

<?php
header('Content-type:application/json;charset=utf-8');
$servername = "localhost:3306";
$database = "database_name";
$username = "user";
$password = "pass";


if($_SERVER["REQUEST_METHOD"] == "POST"){
    $data = $_POST['JSON_VAR'];
    
    if(!empty($data)){
        $conn = mysqli_connect($servername, $username, $password, $database);
        
        if ($conn->connect_error) {
            die("ERROR: " . $conn->connect_error);
        }
        $result = mysqli_query($conn, 'INSERT INTO `game`(`var_name`, `var_value`) VALUES ("SKORE","'.$data.'")', MYSQLI_STORE_RESULT);
        
        if($result){
            echo "OK";
        }else{
            echo "ERROR";
        }
        
        mysqli_close($conn);

    }else{
        echo "NOT EXIST DATA";
    }
}else{
    echo "NO POST";
}

?>
1 Like

But I must point out that this code is really simplified, there is no protection against SQL injection, etc. Just for testing purposes, for sharp operation I would recommend authentication, at least with a token, because after compiling the game the token should not be visible in the code and especially not to users!

Otherwise, the result will be this:
image

Wow! Thank you so much Snyrch for all the time and energy you put into helping me. I really appreciate it. Thank you :slight_smile:

1 Like