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