5. Saving data to databases using Remoting (amfphp-CFM) examples
ColdFusion back-end side
First you need to setup the database. Since steps are the same as the "Loading
data" example, see
there in how to register your database.
Our news.cfc file looks like this:
<cfcomponent>
<cffunction name="insertTitles" access="remote" returntype="string" output="yes">
<cfargument name="Title" type="string" required="true">
<cfargument name="Comments" type="string" required="true">
<cfargument name="Image" type="string" required="true">
<cfquery name="insertTitles" datasource="anastasio">
INSERT INTO Titles (Title, Comments, Image) VALUES ('#arguments.Title#', '#arguments.Comments#', '#arguments.Image#')
</cfquery>
<cfset res = "Ok">
<cfreturn res>
</cffunction>
</cfcomponent>
This is the ColdFusion component of our Remoting side. You can check if it works fine pointing your browser directly to the file. Based on your debug settings, you can see relevant information about service, including possible errors, methods, results and so on. Note that we handle the three arguments and pass to the SQL statement, simply returning an "Ok" string after inserting
AMFPHP back-end side
The amfphp method looks really different from his CFC counterpart. This is due to the reason that PHP build by itself the AMF packet and there are not Server in-build methods to work with. So we need to write our own class with methods defined in it, that means more code. Our file and main class should have the same name as the service. But since there are not build-in methods to look for Flash remoting services, we need to instruct where to find the services explicitly: that's the role of the gateway.php file. This file should be in the same folder as our Flash movie. So
1. Create a gateway.php file
2. Open it in your favorite HTML editor (here, again Dreamweaver is a good helper)
3. Write this code in it:
include "../flashservices/app/Gateway.php";
$gateway = new Gateway();
$gateway->setBaseClassPath("./services/");
$gateway->service();
The first line tells php where to find the library (the flashservices folder) relative to the current file. Then we instantiate the class and in the third line set the base path of the services folder, where PHP expects to find the News services (News.php file) Usually I use services as the name of the folder, but you can change it. Finally, we instantiate the service. Now the Flash movie can access the service. If you get some error, there are some common issues that you can read in the Boards
Once we have the gateway.php in place, we need to write the service, so:
1. Create a file named News.php inside the services folder (or the one you
setup in gateway.php)
2. Open it and write this code in it:
class News
{
//Change this variables to match your needs
var $dbhost = "localhost";
var $dbname = "anastasio";
var $dbuser = "root";
var $dbpass = "";
function News()
{
$this->methodTable = array(
"insertTitles" => array( //insert a row in database
"description" => "Returns avaible Category of books",
"access" => "remote", // available values are private, public, remote
"arguments" => array ("Title", "Comments", "Image"),
"returntype" => "string"
)
);
// Initialize db connection
$this->conn = mysql_pconnect($this->dbhost, $this->dbuser, $this->dbpass);
mysql_select_db ($this->dbname);
}
function insertTitles()
{
$result = mysql_query("INSERT INTO Titles (Title, Comments, Image) VALUES ('$Title', '$Comments', 'Image')")
if ($result) return "Ok";
}
}
Here we create a class that will interact with the amfphp library to insert
data into database. You need to change $dbhost, $dbname, $dbuser and $dbpass
to match your needs. The main function(News) make a description of the methods
and expected parameters, then the insertTitles function simply inserts data,
returning an Ok message as a string.
This is the amfphp part of our Remoting side. You can check if it works fine
pointing your browser directly to the file (i.e. http://localhost/mySite/services/News.php).
If there's no error, but a blank page, all goes fine. If not, check your syntax.
--------------------------------------------------------------------------------------------------------------------------------------
TIP
If you have the error level set to ALL in the php.ini file, probably you get
some warnings. This warnings, while not being fatal errors, break your AMF packet
and Remoting will not work. If you get warnings, check the error level in the
php.ini file.
--------------------------------------------------------------------------------------------------------------------------------------

5 most recent
Flash button as Flex icon
Tree menu
Flash Spell Checker
Flash Remoting Library
MX 2004 Chart/Poll
Articles