Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 6 of 6

Thread: Actionscript 3 and MySQL

  1. #1

    Actionscript 3 and MySQL

    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 /> ";
    }

    }
    ?>
    Last edited by romeoa; October 1st, 2008 at 10:52 AM.

  2. #2
    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.

    Quote Originally Posted by romeoa View Post
    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 /> ";
    }

    }
    ?>

  3. #3
    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

  4. #4
    Actually you can connect directly to mySQL from AS, check out asSQL.

    If you want to go the php route, try ZendAMF

  5. #5
    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.

  6. #6
    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'.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012