1

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();


?>

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

15 Comments

Yes bro, i changed and it shows score = 100 on database, however my aim is to send name and score entered by user. would you pliz help me on this. I appreciate Tq.
I've tried to work with that code u gave above but it looks like it confirms submission to database. coz my idea is to put a button where a user can click to view the stored names and score may you plz modify the code above with a button to lessen my burden Tq.
@mchucha I've updated the answer again. I think you need to do some research into how loading, Events & Listeners work in AS3 before you go much further.
All your ideas works perfect bro, I though still figure out a way to make these data appear in my flash panel. i made a button as u showed me and set all connection seem to work. However, I have to have something to display the data in flash and that is my challenge part. I really do not know how to accomplish this. Help me bro if there is a way. Tq to you again.
@mchucha Simply create a textfield with an instance name of highscores on your stage. You'll need to update your PHP script to return something like echo "scores=<b><center>Best Times:... - so that you're prepending the returned string with scores= and then, in the scoresLoadComplete function, add this line in the try block: highscores.htmlText = returnVars.scores;
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.