0

I was having problems posting scores from a Flash CC AS3 game to a PHP page with SQL database.
I was receiving ERROR #2101 messages in my output window.
After a lot of time searching this problem I was advised to check if PHP was the problem.

I have created a very simple Flash file, the code (I have replaced my actual site address below) is :

import flash.events.MouseEvent;
import flash.net.URLRequest;
import flash.net.URLVariables;
import flash.net.URLLoader;
import flash.events.Event;

var userName = "Jamie";
var newScore = 1300123;

btn_submit.addEventListener(MouseEvent.CLICK, submitted);

function submitted(e:MouseEvent) {
    var myrequest:URLRequest = new URLRequest("http://mysiteaddresshere.com/test_scores.php");
    myrequest.method = URLRequestMethod.POST;
    var variables:URLVariables = new URLVariables();
    variables.player = userName;
    variables.score = newScore;
    var loader:URLLoader = new URLLoader();
    loader.dataFormat = URLLoaderDataFormat.VARIABLES;
    loader.addEventListener(Event.COMPLETE, dataOnLoad);
    loader.load(myrequest);
}

function dataOnLoad(evt:Event) {
    MC_success.alpha = 100;
}

The PHP code is as follows:

<?php

include("db.php");

// Connect to the MySQL server
$link = mysql_connect($host, $user, $pass);
if(!$link) {
    die('Failed to connect to server.'.mysql_error());
}

$db = mysql_select_db($database);
if(!$db) {
    die("unable to select database");
}

$gameName = "ietul";
$employeeID = 123456;
$player = $_POST['userName'];
$score = $_POST['newScore'];

//Create INSERT query
$qry = "INSERT INTO highScore (gameName,employeeID,player,score) VALUES('$gameName','$employeeID','$player','$score')";
$result = @mysql_query($qry);
echo "writing = OK";
exit();
mysql_close();

?>

The include includes the connection and table details for this database.
The game itself should include gameName and employeeID variables, but for the sake of testing I have hardcoded them here.
I upload the game, html and related files to the same site and when I play it, my database updates by adding in the gamename and employeeID details, but the player and score fields are blank.

I have tried different versions of this, using method GET, data format TEXT among others.

I have been staring at this and researching this issue for over 1 week now... if anybody out there can tell me what I'm doing wrong I would be delighted to hear from you !

I'm relatively new to combinging Flash, PHP and SQL so please excuse me if I'm missing something glaringly obvious !

Thanks again,
Jay

Antoine Subit
  • 9,803
  • 4
  • 36
  • 52
JayTray
  • 3
  • 1
  • Did you try to run your PHP directly in the browser by replacing POST variables to some values? – Fergoso Oct 17 '14 at 11:01
  • I have assigned values to variables in PHP and this updates my database, there are 2 variables with values coming from Flash that are not updating. In the above gameName and employeeID are given values in PHP and update correctly but player and score are coming from Flash and do not update on the database, they have zero or blank values. – JayTray Oct 17 '14 at 13:07
  • Try to trace() the return values of your player and score here `function dataOnLoad(evt:Event) {` so you will know if the values were passed on to your php. Also `trace()` values before POSTing here `function submitted(e:MouseEvent) {` so you know if they were sent to the php. So you can narrow the problem. – Fergoso Oct 17 '14 at 14:02

2 Answers2

1

Try this :

AS3 code:

btn_submit.addEventListener(MouseEvent.CLICK, submit_on_Press)

function submit_on_Press(e:MouseEvent) {

    var variables:URLVariables = new URLVariables()
        variables.player = 'player_name'
        variables.score = 1526

    var request:URLRequest = new URLRequest('http://127.0.0.1/score/score.php')
        request.method = URLRequestMethod.POST
        request.data = variables

    var loader:URLLoader = new URLLoader()
        loader.dataFormat = URLLoaderDataFormat.VARIABLES
        loader.addEventListener(Event.COMPLETE, data_on_Load)
        loader.load(request)

}

function data_on_Load(e:Event) {
    trace(e.target.data.writing)
}

PHP code:

<?php

    if(count($_POST) != 2) die();

    $player = trim($_POST['player']);
    $score = intval($_POST['score']);

    $link = mysqli_connect('127.0.0.1', 'root', '', 'test') or die('Error ' . mysqli_error($link)); 

    $query = "INSERT INTO test(player, score) Values('".$player."', ".$score.")";

    $result = $link->query($query);
    $link->close();

    echo 'writing='.($result ? 'ok' : 'error');

?>

For PHP, I recommand you to use mysqli because mysql is deprecated.

akmozo
  • 9,829
  • 3
  • 28
  • 44
0

You need send the variables

myrequest.data = variables; //<------- THIS

Add this to your code

function submitted(e:MouseEvent) {
var myrequest:URLRequest = new URLRequest("http://mysiteaddresshere.com/test_scores.php");
myrequest.method = URLRequestMethod.POST;
var variables:URLVariables = new URLVariables();
variables.player = userName;
variables.score = newScore;
 myrequest.data = variables; //<------- THIS
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.addEventListener(Event.COMPLETE, dataOnLoad);
loader.load(myrequest);

}

esdebon
  • 2,460
  • 5
  • 26
  • 33
  • I've traced the username and score before the submitted function, after assigning variables to them and also in the dataOnLoad function. The trace shows what I expect, the database is updating but player and score are blank. I returned the echo from PHP to Flash and got "writing = ok" which is what I expect. When I change PHP to echo the player or score values, the PHP page is blank, the database updates but player and score are blank again. It looks to me like Flash is connecting and updating but PHP is not taking the variables from Flash? – JayTray Oct 18 '14 at 11:40
  • In your code, you are not sending the variables, add the line myrequest.data = variables – esdebon Oct 20 '14 at 15:37