Skip to main content
Known Participant
November 26, 2012
Answered

AS2 - Problem with inserting score/name to mysql/database

  • November 26, 2012
  • 1 reply
  • 4234 views

Hello,

First of all.. sorry that I havn't put the AS in a box, not sure how to do it on this forum!

Okey so this is my problem:

I want to sumbit my score and name into my database.

This is the script I have tried in Flash.

stop();

//create loadVars Objects

playerx = new LoadVars();

playerRegistered = new LoadVars();

playerRegistered.onLoad = showResult;

//create button handler code to call function which sends & loads php

_root.submitt.onRelease = submitScore;

function submitScore():Void {

            playerx.name = name_txt.text;

            playerx.score = score_txt.text;

            playerx.sendAndLoad("register.php",playerRegistered);

}

This is the PHP script:

<?php

$connect = mysql_connect('x.com.mysql','x','password');

mysql_select_db('x', $connect);

$sql = 'INSERT INTO high_scores_avoidthecars(name,score) VALUES ("'.$_POST['name'].'","'.$_POST['score'].'")';

?>

Nothing gets posted to the database, no error etc.. any ideas?

This topic has been closed for replies.
Correct answer Ned Murphy

Bump*

So bored of not getting this.. gahhh

xmasgame wrote:

Checked the link but I'm a little clueless when it comes to Flash -> php -> database, to be honest..

I have searched all day long, no good tutorials, no good explainings to be honest.

I do not know why, but I find this hard to understand.. even if I know how to do this without problems from HTML and PHP

Hope someone could explain me as I'm running out of places to find it out.


If you went thru the link I provided you should have seen where you need to make changes to the code you showed to get the Flash code working.  Here is a link to a working example that reflects the second part of the code of that link that involves sending data (starting at line 28) with your textfields/etc.

http://www.nedwebs.com/Flash/register.html

The main problem with your AS2 code involves the following line...

     playerx.sendAndLoad("register.php");

it should be...

     playerx.sendAndLoad("register.php", playerx. "POST");

Here is all of the code I used for the example files I made...

AS2 CODE

playerx = new LoadVars();

_root.submitt.onRelease = submitScore;


function submitScore():Void {

            response.text =  "clicked\n";

            playerx.name = name_txt.text;

            playerx.score = score_txt.text;

            playerx.sendAndLoad("register.php", playerx, "POST");

}

playerx.onLoad = onLoadCallBack;

function onLoadCallBack(succes)
{
      response.text += "onLoad Processed\n";


      if(succes) {
           response.text += "Success\n";
           response.text += this.lVar1+"\n";
           response.text += this.lVar2;
      } else {
           response.text = "Loading Error!!";
      }
}

PHP CODE

<?
// get variables
$var1 = $_POST['name'];
$var2 = $_POST['score'];

// send variables
echo "&lVar1=$var1 returned&";
echo "&lVar2=$var2 returned as well&";
?>

1 reply

Ned Murphy
Legend
November 26, 2012

I am not sure why you are creating two LoadVars instances.  You should only need the playerx one.  Here's a link to some example code.  See if adjusting yours to match it helps.

http://snipplr.com/view/8878/

xmasgameAuthor
Known Participant
November 26, 2012

Have made a change, only need one LoadVar actually:

stop();

//create loadVars Objects

playerx = new LoadVars();

_root.submitt.onRelease = submitScore;

function submitScore():Void {

            playerx.name = name_txt.text;

            playerx.score = score_txt.text;

            playerx.sendAndLoad("register.php");

}

I think my problem is like this:

Connection:

PHP -> Database = Working

Flash -> PHP = Doesn't work