Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,367 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,298 people online right now. Registration is fast and FREE... Join Now!




Actionscript 3 and MySQL

 
Reply to this topicStart new topic

Actionscript 3 and MySQL

romeoa
post 1 Oct, 2008 - 06:09 AM
Post #1


New D.I.C Head

*
Joined: 10 Mar, 2007
Posts: 3


My Contributions


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.

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



And here is my php code.

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

}
?>


User is offlineProfile CardPM

Go to the top of the page

DilutedImage
post 1 Oct, 2008 - 09:48 AM
Post #2


D.I.C Addict

Group Icon
Joined: 20 Nov, 2006
Posts: 642



Thanked 6 times

Dream Kudos: 25
My Contributions


I'll dig into this a little later this afternoon. In the meantime, you may want to check out AMFPHP (though their http server seems to be down at the moment). It's a great way to share data and functions between Flash and PHP.
User is offlineProfile CardPM

Go to the top of the page

romeoa
post 2 Oct, 2008 - 05:33 AM
Post #3


New D.I.C Head

*
Joined: 10 Mar, 2007
Posts: 3


My Contributions


Thanks. I'll check out the AMFPHP site. Also, you can view the test flash file that I am working on here.

thanks in advance.

-Romeo


User is offlineProfile CardPM

Go to the top of the page

DilutedImage
post 4 Oct, 2008 - 02:26 PM
Post #4


D.I.C Addict

Group Icon
Joined: 20 Nov, 2006
Posts: 642



Thanked 6 times

Dream Kudos: 25
My Contributions


Sorry for the delayed response; things got busy.

You're missing the closing bracket for the VALUES in your query:
CODE
$result = mysql_query("INSERT INTO thoughtentry(headline, content) VALUES('$headlineCopy', '$contentCopy')");

That'll fix your database error, and successfully save the data. Once you've saved the data, and queried it back out, what do you plan to do with it? If you're wanting to be able to access it in Flash, then you'll want to prepend the output with a variable name. Perhaps something like this:
CODE
if($result = mysql_query($query)) {
    $output = 'entries=';
    while ($row = mysql_fetch_array($result)) {
        $output .= "{$row['headline']}<br />{$row['content']}<br /><br />";
    }
    echo($output);
}



This post has been edited by DilutedImage: 4 Oct, 2008 - 02:26 PM
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/22/08 05:10AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month