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

[kml_flashembed movie="/labs/swf/phpwritetodb.swf" height="150" width="450" /]

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:
  1. var submitListener:Object = new Object();
  2. submitListener.click = function(evt:Object) {
  3.     try {
  4.         checkForm();
  5.     }
  6.     catch(e) {
  7.         result_ta.text = e.toString();
  8.         return;
  9.     }
  10.     var result_lv:LoadVars = new LoadVars();
  11.     result_lv.onLoad = function(success:Boolean) {
  12.         if (success) {
  13.             trace("THE ERROR = " + result_lv.error);
  14.             trace(unescape(result_lv.toString()));
  15.             if (result_lv.error != undefined) {
  16.                 result_ta.text = result_lv.error;
  17.             }else{
  18.                 result_ta.text = "Wrote to DB: \n";
  19.                 result_ta.text += "First Name = " + result_lv.fname + "\n";
  20.                 result_ta.text += "Last Name = " + result_lv.lname + "\n";
  21.                 result_ta.text += "Age Name = " + result_lv.age + "\n";
  22.                 result_ta.text += "Total Records = <b>" + result_lv.nuRows + "</b>";
  23.             }
  24.         } else {
  25.             result_ta.text = "Error connecting to server.";
  26.         }
  27.     }
  28.     var send_lv:LoadVars = new LoadVars();
  29.     send_lv.fname = fn_tfl.text;
  30.     send_lv.lname = ln_tfl.text;
  31.     send_lv.age = age_tfl.text;
  32.     send_lv.sendAndLoad("path/to/your/php/file.php", result_lv, "POST");
  33. };
  34. submit_btn.addEventListener("click", submitListener);
  35. function checkForm() {
  36.     if (fn_tfl.text == undefined || fn_tfl.text.length <1) {
  37.          throw new Error("First name is required");
  38.     }
  39.     if (ln_tfl.text == undefined || ln_tfl.text.length <1) {
  40.         throw new Error ( "Last name is required");
  41.     }
  42.     if (age_tfl.text == undefined || age_tfl.text.length <1) {
  43.         throw new Error ( "Age is required");
  44.     }
  45. }

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

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

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

9 Responses

  1. LB Says:

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

  2. Newbie Says:

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

  3. Helmut Says:

    @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.

  4. Derry Says:

    Is this AS2 or AS3?

    Thanks!

  5. Helmut Says:

    Good ol' AS2

  6. Lee Says:

    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?

  7. Helmut Says:

    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.

  8. Abs Says:

    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

  9. Helmut Says:

    @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.

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.