advertisement

Sorry it has been a while but anyway, in this tutorial I will be teaching you how to make flash talk to a database through PHP. So, on with the tutorial…

First thing to do is open up flash. When you have flash open, create a new file, call it add.fla, it can be any size you like and any fps (not important here any way). Now we have the file, select the first frame on the timeline, now open up the actions panel (F9) and add this:
[as]
stop();
[/as]

Now, draw two input text fields on the stage, make one one line and make the other multiple lines, like the example below:

boxes
Now draw two static text boxes next to the two text boxes you have already drawn, in the top one write Name and in the bottom one write Message.

Now select the top input text box and in the properties panel at the bottom of the workspace, you will see a bit that says Var: and in has a text field. In that text box (this is for the top input text field) put “name2” (without quotes). So the box is selected and the var box looks like this:
text field var

Do the same to the bottom text box but give that a var of “msg” (without quotes).
So now your stage should look like this:
stage

OK, now we need to add a button to our stage, so use the rectangle tool to draw a rectangle on the stage, select the text tool and create a static text box and write submit over the rectangle you drew. Select the text and the rectangle and convert them to a symbol (F8), give it the “Button” behaviour, call it anything and the registration doesn’t matter. (If you are having trouble making the button, refer to my button tutorial).

Now, select the button you made and open the actions panel (F9), now in the actions panel, add this code:
[as]
on (release) {
loadVariablesNum (“add.php”, 0, “POST”);
gotoAndStop(2);
}
[/as]
What this does is: The first line checks if the button is pressed. The second line calls out to a file, (“file.ext”, level, method);. The third line goes to the next frame and stops (we will add this next). The fourth line terminates the code.

Now add a new frame, select the frame and add:
[as]
stop();
[/as]
in the actions panel.

In the frame put some message to show it was succesful and a link to the location of a file that we will create soon (it will be called view.php) – http://location.com/folder/view.php – or something like that. Now publish your movie as a swf file (F12).

Little bit of mySQL quickly just to create the table needed for data holding.
[mysql]
CREATE TABLE `database name`.`flashnphp` (
`name` VARCHAR(30) NOT NULL,
`msg` VARCHAR(45) NOT NULL
)
ENGINE = InnoDB;

[/mysql]
The bit the says `database name` you must change to your database name. The code above creates a table with a column called name and a column called msg.

Now we will move to PHP.

Open up your favorite text editor and create a new file called add.php, make sure you save it to the same folder as add.fla.

add.php

<?php
//connecting
$dbname = "DBNAME";
$dbloc = "localhost";
$dbuser = "DBUSER";
$dbpass = "DBPASS";
mysql_connect($dbloc, $dbuser, $dbpass) or die (mysql_error());
mysql_select_db($dbname) or die (mysql_error());
//end connecting

$name = strip_tags($HTTP_POST_VARS['name2']);
$msg = strip_tags($HTTP_POST_VARS['msg']);
$insert_str  = "INSERT INTO flashnphp(name, msg) VALUES('" . $name . "', '" . $msg . "')";
$mysql = mysql_query($insert_str);

if($mysql){
echo "done";
}
else
{
echo "error :" . mysql_error();
}
?>

Ok, as you know,

<?php ?>

open and close php.

//connecting
$dbname = "DBNAME"; // database name goes here
$dbloc = "localhost"; // database location/host goes here
$dbuser = "DBUSER"; // database username goes here
$dbpass = "DBPASS"; // database password goes here
mysql_connect($dbloc, $dbuser, $dbpass) or die (mysql_error());
mysql_select_db($dbname) or die (mysql_error());
//end connecting

This section of the code just connects to the database using the variables set at the start of the code. Remember to change the variables to your database info.

$name = strip_tags($HTTP_POST_VARS['name2']);
$msg = strip_tags($HTTP_POST_VARS['msg']);

Two variables that equal the variables of the text fields in the flash movie.

$insert_str  = "INSERT INTO flashnphp(name, msg) VALUES('" . $name . "', '" . $msg . "')";
$mysql = mysql_query($insert_str);

The first variable set runs a mysql command to insert the data called from the flash movies by the two variables, $name and $msg into the table that we created earlier in the tutorial.
The second variable runs the first variable as a mysql command.

if($mysql){
echo "done";
}
else
{
echo "error :" . mysql_error();
}

The first line checks if the $mysql variable has run and if it has, continue ( { ) and if all goes well, write done ( echo “done”; ) then terminate ( } ). But if there is a problem ( else { ) write error and the the thing that has gone wrong ( echo ” error :” . mysql_error(); ) then terminate else ( } ).

Now we have the file that collects the data and the file that sends the data, what we need now is the file to view the data, we will call this view.php.

view.php

<?php
//connecting
$dbname = "DBNAME";
$dbloc = "localhost";
$dbuser = "DBUSER";
$dbpass = "DBPASS";
mysql_connect($dbloc, $dbuser, $dbpass) or die (mysql_error());
mysql_select_db($dbname) or die (mysql_error());
//end connecting

$result = mysql_query("SELECT name, msg FROM flashnphp");
while($r = mysql_fetch_array($result))
{
$name=$r["name"];
$msg=$r["msg"];
echo "$name<br />
$msg";
}
?>

Again the first 8 lines is just connecting to the database.

$result = mysql_query("SELECT name, msg FROM flashnphp");

This sets a variable that selects data from the two columns in the table.

while($r = mysql_fetch_array($result))
{
$name=$r["name"];
$msg=$r["msg"];
echo "$name<br />
$msg";
}

The first line says while $r fetches an array from the table using mysql and the $result variable set earlier.

$name=$r["name"];
$msg=$r["msg"];

These two lines are setting variables that get results from the columns in the table using the $r variable.

echo "$name<br />
$msg";

That just echoes the data from the database.

}

Ends it.

Note: This is a very simple way to make flash work with PHP, there are much better ways to do it but this is just an introduction really.

Well, I guess that’s the tutorial, you better like it because it took ages to write 😀
If you have any questions, feel free to email me at webmaster@flashdoom.net and I will try and get back to you A.S.A.P or just post a comment.

Thanks for reading,
mortisimus
http://www.flashdoom.net

advertisement