Reputation: 33
am trying to connect my flash project with database. However, i want to be able to submit and retrieve data from database having 3 fields (name, score and date). My problem is that if I click submit button and when i go to check in database i only see 0 score meaning nothing is submitted. Can someone help me on this. Tq Here is my coding for flash:
var str:String = "";
var myscore = 0;
btn_submit.addEventListener(MouseEvent.CLICK, submitted);
function submitted(e:MouseEvent)
{
var myrequest:URLRequest = new URLRequest("http://127.0.0.1/Y/sendscore.php");
myrequest.method = URLRequestMethod.POST;
var variables:URLVariables = new URLVariables();
variables.name = str;
variables.score = myscore;
myrequest.data = variables;
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;
//status is a custom flag passed from back-end
}
MY PHP CODE FOR SENDING DATA;
<?php
//Include database connection details
require_once('config.php');
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
//Function to sanitize values received from the form. Prevents SQL injection
function clean($str) {
$str = @trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return mysql_real_escape_string($str);
}
//Sanitize the POST values
$name = clean($_POST['name']);
$score = clean($_POST['score']);
$currentdate = date("Y/m/d");
//Create INSERT query
$qry = "INSERT INTO highscores(user, score, date) VALUES('$name','$score','$currentdate')";
$result = @mysql_query($qry);
echo "writing=Ok";
exit();
mysql_close();
?>
MY PHP CODE TO RETRIEVE DATA;
<?php
//Include database connection details
require_once('config.php');
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
//Create INSERT query
$qry = "SELECT * FROM highscores ORDER BY score ASC";
$result = @mysql_query($qry);
$num=mysql_numrows($result);
if($num > 10)
{$num = 10;}
//echo "writing=Ok";
echo "<b><center>Best Times:</center></b><br /><table>";
$i=0;
$i2=1;
while ($i < $num) {
$name=mysql_result($result,$i,"user");
$time=mysql_result($result,$i,"score");
$date=mysql_result($result,$i,"date");
echo "<tr><td align=left valign=top>$i2.</td><td align=center valign=top><b>$name</b> | $score | $date</td></tr><tr><td colspan=2><hr></td></tr>";
$i2++;
$i++;
}
echo "</table>";
//$urlRefresh = "scores.php";
//header("Refresh: 15; URL=\"" . $urlRefresh . "\"");
exit();
mysql_close();
?>
Upvotes: 1
Views: 1194
Reputation: 5925
Unless you've missed out some piece of code whereby the score is changed, you're actually declaring var myscore = 0
right at the top of your AS3 code block.
The first thing would be to change that to 100
, then run you script and see if that modified score variable is getting submitted. If it is, then everything is working as it should.
Update:
You've changed your question, and you want to be able to load data. You already have a function set up for this via loader.addEventListener(Event.COMPLETE, dataOnLoad)
. You just need to grab the data your PHP script is sending back. This can be accessed via your evt
parameter in the dataOnLoad
function:
function dataOnLoad(evt:Event):void {
trace("Data submission complete");
var returnVars = evt.target.data;
trace("***********************");
for (var myVars in returnVars) {
trace(myVars + ": " + returnVars[myVars]);
}
trace("***********************");
}
Update 2:
You've requested help with loading your scores from the database. As you already have a PHP file that retrieves this from the database (let's assume it's called scores.php
),m you just need a function in Flash to load it.
You already have the basic functions in place, making use of URLLoader
and Event Listeners. You just need these to apply to a straightforward load:
btn_scores.addEventListener(MouseEvent.CLICK, loadScores);
function loadScores(e:MouseEvent):void {
var fileLoader:URLLoader = new URLLoader();
fileLoader.addEventListener(Event.COMPLETE, scoresLoadComplete);
fileLoader.load(new URLRequest("scores.php"));
}
function scoresLoadComplete(evt:Event):void {
try {
var returnVars = evt.target.data;
trace("***********************");
for (var myVars in returnVars) {
trace(myVars + ": " + returnVars[myVars]);
}
trace("***********************");
} catch (err:Error) {
trace("Can't parse loaded file: " + err.message);
}
}
Note that your PHP file currently returns an HTML table of results. This won't behave in Flash; you'd be much better off sending through key/value pairs and parsing them or just a basic HTML list of scores.
Upvotes: 1