Flash PHP mySQL – Write to a Database with Flash and PHP

I have written a small script that will assist you into using communication between Flash and PHP to write to a mySQL database. The task is simple, verify the contents of the fields in flash and then send the information to PHP who then writes the information into the database.

ActionScript:
    var submitListener:Object = new Object();
    submitListener.click = function(evt:Object) {
       try {
          checkForm();
       }
       catch(e) {
          result_ta.text = e.toString();
          return;
       }
       var result_lv:LoadVars = new LoadVars();
       result_lv.onLoad = function(success:Boolean) {
          if (success) {
             trace("THE ERROR = " + result_lv.error);
             trace(unescape(result_lv.toString()));
             if (result_lv.error != undefined) {
                result_ta.text = result_lv.error;
             }else{
                result_ta.text = "Wrote to DB: \n";
                result_ta.text += "First Name = " + result_lv.fname + "\n";
                result_ta.text += "Last Name = " + result_lv.lname + "\n";
                result_ta.text += "Age Name = " + result_lv.age + "\n";
                result_ta.text += "Total Records = <b>" + result_lv.nuRows + "</b>";
             }
           } else {
             result_ta.text = "Error connecting to server.";
           }
       }
       var send_lv:LoadVars = new LoadVars();
       send_lv.fname = fn_tfl.text;
       send_lv.lname = ln_tfl.text;
       send_lv.age = age_tfl.text;
       send_lv.sendAndLoad("path/to/your/php/file.php", result_lv, "POST");
    };
    submit_btn.addEventListener("click", submitListener);
    function checkForm() {
       if (fn_tfl.text == undefined || fn_tfl.text.length < 1) {
           throw new Error("First name is required");
       }
       if (ln_tfl.text == undefined || ln_tfl.text.length < 1) {
          throw new Error ( "Last name is required");
       }
       if (age_tfl.text == undefined || age_tfl.text.length < 1) {
          throw new Error ( "Age is required");
       }
    }

This is the script used to create the tables and fields:

PHP:
    CREATE TABLE `users` (<br />
    `id` INT( 6 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,<br />
    `fname` VARCHAR( 15 ) NOT NULL ,<br />
    `lname` VARCHAR( 15 ) NOT NULL ,<br />
    `age` INT( 3 ) NOT NULL<br />
    ) ENGINE = MYISAM ;

Make sure you check the mySQL version on your server in case you encounter any errors.

17 Responses to “Flash PHP mySQL – Write to a Database with Flash and PHP”

  1. LB

    This is wicked. Helmut, you’re the man.

    Reply
  2. Newbie

    Where can i find the “file.php” or how will that look?

    Reply
  3. Helmut

    @Newbie

    The “file.php” is a file that you write in PHP which inserts the information into the db, the name was used just as an example but it can be named anything you like.

    Reply
  4. Derry

    Is this AS2 or AS3?

    Thanks!

    Reply
  5. Helmut

    Good ol’ AS2

    Reply
  6. Lee

    Just before I go ahead and try this out. can you use ajax to send this so we can write the DB without having to reload the page?

    Reply
  7. Helmut

    Hey Lee,

    Not sure why you would need AJAX since all the information is being handled within Flash you don’t need to refresh the browser to see the updates.

    Go ahead and test the form in the post and you will see what i mean.

    Reply
  8. Abs

    Hey, am kinda new into this, and I believe it is something like what I wanna do..
    Am making a wbsite with Flash, and I wanna add a guest book for people to commets. All I nd is to store that in database.
    My question is, if that can help with what am trying to do, then where am I suppose to place the script ?? And how can I add the HTML code to the Flash ?!
    Thank you

    Reply
  9. Helmut

    @abs

    the code above is for people that have a little bit of experience with flash/php/html that is why I didnt post details on how to implement the code. You can use the same idea for a guestbook as you commented. Same you would save the user’s name and comment to the database and then pull it with flash.

    Reply
  10. Jasen

    How about a way to allow users to select one of three options and then pay using paypal, and have their account be active as long as they are paid up for that month / year. If they do not pay, or they expire, they will be suspended which gives them no access until they pay.

    all flash? possible?

    Reply
  11. Helmut

    Yes, it is possible.

    Reply
  12. hendrayana

    dear Mr. Helmut pelase gave us the php file sample to easy undertanding :)

    Reply
  13. Tony

    This is a great code! However, I need something that will work in AS3, and I can’t find a way to use the PHP “POST” method in AS3, can anyone help with this?

    Reply
  14. johnny

    WTF would you post how to do this without including the PHP file? Youre dumb.

    Reply
    • Helmut Granda

      Sorry Johnny, but the source to create the tables is there and the rest is up to you and your imagination.

      Reply
    • Tommy

      Oh Johnny, the web is full of half-wits like yourself. If you are writing code at this level and can;t figure out the PHP, you should go dig up your Lite Brite and stick with that…

      Reply
  15. Knigh7

    can you please post or email me the source

    Reply

Leave a Reply