问题
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
回答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.
回答2:
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);
}
来源:https://stackoverflow.com/questions/26422299/submitting-scores-from-as3-to-php-sql-error-2101