Mike Potter

February 07, 2006

Flex and PHP - A simple sample

I also posted this to the Adobe Flex Builder forum...

I wrote up a small Flex Builder 2.0 sample that uses PHP to update a MySQL database, with the front end being a Flex based application. In this very simple example, we have two fields, username and email address, and a datagrid. The username and email address fields are text fields, and filling them in and clicking on the "Submit" button adds them to the database. The data grid displays all the users, with an ID and their username. Below that, a text field shows the email address of the selected user (I did this, rather than 3 columns in the datagrid, to show binding data to another field, and I think its cool) :)

Here's the code: (I don't know how to attach files to this forum!)

MySQL schema:
Note: The PHP code assumes that you have created the following table in a database called "sample":
CREATE TABLE `users` (
`userid` int(10) unsigned NOT NULL auto_increment,
`username` varchar(255) collate latin1_general_ci NOT NULL,
`emailaddress` varchar(255) collate latin1_general_ci NOT NULL,
PRIMARY KEY (`userid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=3 ;

MXML:



private function send_data():void {
userRequest.send();
}
]]>





















{username.text}{emailaddress.text}


request.php:
NOTE: You'll likely have to change the default "username" and "password".

//connect to the database
$mysql = mysql_connect(localhost, "username", "password");

mysql_select_db( "sample" );
//if the username and email address are filled out
if( $_POST["emailaddress"] AND $_POST["username"])
{
//add the user
$Query = "INSERT INTO users VALUES ('', '".$_POST['username']."', '".$_POST['emailaddress']."')";

$Result = mysql_query( $Query );
}

//return a list of all the users
$Query = "SELECT * from users";
$Result = mysql_query( $Query );

$Return = "";

while ( $User = mysql_fetch_object( $Result ) )
{
$Return .= "".$User->userid."".$User->username."".$User->emailaddress."";
}
$Return .= "";
mysql_free_result( $Result );
print ($Return)
?>

I'll be posting other tutorials for Flex and PHP in the near future on many of the PHP focused websites. If you have comments, I'd appreciate them here before I send out samples to those sites.

Posted by Mike Potter at 03:09 PM on February 07, 2006

Comments

Marc Van Norden — 04:39 PM on February 07, 2006

I am trying to grasp flex development. I have touched php and msyql. And this tutorial among others you may have will help send me in the right direction. Where are your other postings?

Irishdancer — 04:56 PM on February 07, 2006

where did all the MXML code go to? *wonders*

Evert — 05:01 PM on February 07, 2006

Nice, but very insecure.. you left a major hole for SQL query injection.

jf.sal — 06:10 PM on February 07, 2006

Thanks

Some mxml code is missing.

The best way to show flex code is to explore the new beta tool, Publish: View Source in Flash Player

Add your comments

Remember Me?

(You may use HTML tags for style.)