Part 2: Writing data to MySQL using Flash/PHP

In order to keep a record of all the top scores, we need a way to write data to the database! Now that we have confirmed that the database was imported correctly, we can move on to writing data to the MySQL database. In the Flash game I developed, the user inserts a name after completing the three rounds. Once this happens, the game sends the user data to a PHP script which in turns writes the data to the MySQL table. Let’s look at the code that will accomplish this.

  1. Open up a new Flash File (Actionscript 3.0). Press F9 and paste in the following code into the Actions - Frame. This code uses what is known as URL variables to send data to the PHP script. If you don’t know what URL variables are, you might want to check out this quick tutorial here. One of the lines must be modified with the address of the accompanying php file. The accompanying php file is shown in the next step.
    import fl.controls.TextInput;
    import fl.controls.TextArea;
    var variables:URLVariables = new URLVariables();
    variables.name = "Zzyzx";
    variables.roundOne = 1.341;
    variables.roundTwo = 2.111;
    variables.roundThree = 2.222;
    variables.final = 1.341+2.111+2.222;
     
    var request:URLRequest = new URLRequest();
     
    ////insert in the location of the php script ////////////////
    request.url = "insert the web address of php file here";
    //////////////////////////////////////////////////////////////
     
    request.data = variables;
    var loader:URLLoader = new URLLoader();
     
    loader.load(request); //sends the request 
     
    //when the request is done loading, it goes to the completeWriting function
    loader.addEventListener(Event.COMPLETE, completeWriting);
     
    function completeWriting(event:Event):void {
    	var writingCompleted:TextField = new TextField;
    	writingCompleted.autoSize = "center";
    	writingCompleted.x =200;
    	writingCompleted.y= 200;
    	writingCompleted.text = event.target.data;
    	addChild(writingCompleted);
     
    }
  2. Now that we have the code to send the data within flash to the PHP script, it might be useful to write the actual PHP script! The following script takes in URL variables from flash, and then writes the pertinent information to the MySQL database. Copy and paste this code, and save it as a php file. Remember to fill in the information for the host, username, password, etc.
    <?php
    ////////////////// modify this information ///////////////////////
      $host = "localhost";
      $user = "";
      $pass= "";
      $table = "";
      $database = "";
    ///////////////////////////////////////////////////////////////////////
     
    //stores the URLvariables into variables that php can use
    $one = $_GET['name']; 
    $two = $_GET['roundOne']; 
    $three= $_GET['roundTwo']; 
    $four = $_GET['roundThree']; 
    $five = $_GET['final'];
     
      // Connects to the database server
      $dbcnx = @mysql_connect($host, $user, $pass);
      if (!$dbcnx) {
        echo( "<P>Unable to connect to the database server at this time.</P>" );
        exit();
      }
     
      // Selects the database
      if (! @mysql_select_db($database) ) {
        echo( "<P>Unable to find database");
        exit();
      }
     
    //this is the command used to write the record into the MySQL database
    $query="INSERT into {$table} (name,roundOneTime, roundTwoTime,roundThreeTime, 
    finalTime) VALUES ('{$one}', {$two}, {$three},{$four},{$five})";   
     
    //executes the command
    mysql_query($query) or die("Data not written.");
    echo("The data has been written to the table!");
    ?>
  3. Next, upload the PHP file onto your website. Also, remember to fill in the following line correctly on the flash code shown here, or else the program will not work:

    ////insert in the location of the php script //////////
    request.url = “insert the web address of php file here”; ///////////////////////////////////////////////////////

  4. Now, go to Adobe Flash, and run the application by pressing CTRL + Enter. If you did everything correctly, then the text box should be populated with the following text:

    The data has been written to the table.

    Congratulations, you just used Flash CS3 (Actionscript 3.0) and PHP to write data to the MySQL database! Try to take a moment to examine the code to figure out how it was done.

  5. If the information above doesn’t appear, then that means that you have either imported the table incorrectly, did not assign the correct privileges to the user, or your information in the php file is incorrect. I recommend you go back and fix things before proceeding any further.
  6. So what exactly is going on here? Essentially, Flash is sending URL variables to the PHP script. The PHP script uses these variables and sends a query to the MySQL database to add a new record. After the data is successfully recorded into the MySQL database, the PHP script accordingly tells Flash that the data was successfully written.
  7. If you want to be sure that the data was actually written to the MySQL database. You can always use phpMyAdmin to check that the last entry in the table is what you expect it to be. I recommend that you do this to make sure that the record was actually written to the database.

Pages: 1 2 3 4 5