PDA

View Full Version : Actionscript 3 and MySQL



romeoa
October 1st, 2008, 10:44 AM
Hi,

I am trying to get Flash to insert info into a table in a MySQL DB. So far I am not having any luck. I am using PHP as the in between.
Any help would be great.

Thanks in advance,

Here is my actionscript 3 code.




stop();

submit.addEventListener(MouseEvent.CLICK, sendData)
function sendData(evt:MouseEvent){
if(totwHeadline.text!="" && totwContent.text !=""){
var myData:URLRequest = new URLRequest("insert.php")
myData.method = URLRequestMethod.POST
var variables:URLVariables = new URLVariables()
variables.Headline = totwHeadline.text
variables.Content = totwContent.text
myData.data = variables
var loader:URLLoader = new URLLoader()
loader.dataFormat = URLLoaderDataFormat.VARIABLES
loader.addEventListener(Event.COMPLETE, dataOnLoad)
loader.load(myData)
} else status_txt.text = "All fields are mandatory";
}
function dataOnLoad(evt:Event){
if(evt.target.data.writing=="Ok") {
gotoAndStop(2);
} else status_txt.text = "Error in saving submitted data";
}



Here is my php code.


<?php
//Capture data from $_POST array
$headlineCopy = $_POST['Headline'];
$contentCopy = $_POST['Content'];
//Connection to database
$connect = mysql_connect("localhost", "username", "password");
mysql_select_db ("totw", $connect);
//Perform the query
$result = mysql_query("INSERT INTO thoughtentry(headline, content) VALUES('$headlineCopy', '$contentCopy'");
if($result) echo "writing=Ok&";
else echo "writing=Error";




$query = 'SELECT * FROM thoughtentry';


if($r = mysql_query($query))
{
while ($row = mysql_fetch_array ($r))
{
print "{$row['headline']} <br /> {$row['content']}<br /> <br /> ";
}

}
?>

r3dkrpto
October 1st, 2008, 11:23 AM
I don't see anything wrong with your code offhand. Do a sanity check first. In the php, print the $_POST array with print_r($_POST) to see if what you expected was passed. Print any errors the mysql_query might have produced and in the AS3 code, do a trace(evt.target.data) to see what the PHP code did. Make sure the php file was even executed.

Just keep throwing in trace statements or run this in debug mode until you understand what's going on.


Hi,

I am trying to get Flash to insert info into a table in a MySQL DB. So far I am not having any luck. I am using PHP as the in between.
Any help would be great.

Thanks in advance,

Here is my actionscript 3 code.




stop();

submit.addEventListener(MouseEvent.CLICK, sendData)
function sendData(evt:MouseEvent){
if(totwHeadline.text!="" && totwContent.text !=""){
var myData:URLRequest = new URLRequest("insert.php")
myData.method = URLRequestMethod.POST
var variables:URLVariables = new URLVariables()
variables.Headline = totwHeadline.text
variables.Content = totwContent.text
myData.data = variables
var loader:URLLoader = new URLLoader()
loader.dataFormat = URLLoaderDataFormat.VARIABLES
loader.addEventListener(Event.COMPLETE, dataOnLoad)
loader.load(myData)
} else status_txt.text = "All fields are mandatory";
}
function dataOnLoad(evt:Event){
if(evt.target.data.writing=="Ok") {
gotoAndStop(2);
} else status_txt.text = "Error in saving submitted data";
}



Here is my php code.


<?php
//Capture data from $_POST array
$headlineCopy = $_POST['Headline'];
$contentCopy = $_POST['Content'];
//Connection to database
$connect = mysql_connect("localhost", "username", "password");
mysql_select_db ("totw", $connect);
//Perform the query
$result = mysql_query("INSERT INTO thoughtentry(headline, content) VALUES('$headlineCopy', '$contentCopy'");
if($result) echo "writing=Ok&";
else echo "writing=Error";




$query = 'SELECT * FROM thoughtentry';


if($r = mysql_query($query))
{
while ($row = mysql_fetch_array ($r))
{
print "{$row['headline']} <br /> {$row['content']}<br /> <br /> ";
}

}
?>

sixtuslab
December 28th, 2008, 02:13 PM
I'm doing a form in AS3 developed in Flex and compiled in CS3, I guess we read the same tutorials as I have a "writing" variable also. When I edited it to be echoed out of a PHP with an added variable

echo "&writing=Ok&s=s";

I got access to the writing's value (string) in CS3 when I test movie but when I publish and send the form in a browser the MySQL data gets saved correctly but the "loader.addEventListener(Event.COMPLETE" function doesn't fire

Tomi

substance
December 28th, 2008, 06:24 PM
Actually you can connect directly to mySQL from AS, check out asSQL (http://code.google.com/p/assql/).

If you want to go the php route, try ZendAMF (http://gotoandlearn.com/play?id=90)

scottc
December 29th, 2008, 12:46 AM
I wouldn't recommend using asSQL unless you know what you are doing. (ie limited user account with SELECT only permissions.)

They could easily obtain your db password and run all sorts of queries otherwise, having a server side language in between is bestest.

sixtuslab
December 29th, 2008, 04:16 AM
I'll keep using PHP, the MySQL stuff on my flash app works very smoothly with PHP, the only bizarre thing is that I can recieve variables from PHP back to flash but the flash URLLoader that handles the variables doesn't fire the Event.COMPLETE function 'sendComplete'.