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

[as]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 = " + result_lv.nuRows + ""; } } 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"); } }[/as]

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

[php]CREATE TABLE `users` (
`id` INT( 6 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`fname` VARCHAR( 15 ) NOT NULL ,
`lname` VARCHAR( 15 ) NOT NULL ,
`age` INT( 3 ) NOT NULL
) ENGINE = MYISAM ;[/php]

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