Tag Archives: Mysql

Flash(AS2) + PHP + MySql

Last couple of month was working on Flash. Became familiar with Flash and started loving it… 🙂 Then a project came they
asked for making CMS for their flash site. You can see the site http://www.zfp.com

And the game started… 🙂

Started learning AS2 and loadvars. Faced lots of difficulties and finally manged to communicate with Flash(AS2) + PHP + Mysql.
In this post I will try to give basic idea to communicate Flash(AS2) and PHP.

1. Flash(AS2)

lv_th = new LoadVars();

lv_th.onData = function(varText:String)

{

 temp = varText.split('@@');

 photo= temp[0].split('|');

 photo_title = temp[1].split('|');

 photo_description = temp[2].split('|');

};

lv_th.load("photo_gallery.php?gallery_id=5");


1. PHP+MySql


$gal_id = $_GET['gallery_id'];
$sql = "SELECT * FROM gallery WHERE gal_id=$gal_id ORDER BY gal_id";
$result = mysql_query($sql) or die(mysql_error());
$photo = array();
$photo_title = array();
$photo_description = array();

while($rows = mysql_fetch_object($result))
{
    $photo[]             = $rows->photo;
    $photo_title[]       = $rows->title;
    $photo_description[] = $rows->description;
}

print implode('|',$photo) . '@@' . implode('|',$photo_title) . '@@' .
implode('|',$photo_description);


First one is flash AS2 Code to get the data from php. It first create a new instance LoadVars();

then onload called the photo_gallery.php?gallery_id=5 and wait for the response to come.

In php we made the query based on the gallery_id and get the data and put it in three separate

array. then we implode each array values with ‘|’ and separate each array with ‘@@’.

In flash after getting the data as a string in varText variable we split it by ‘@@’ to separate the

different array values and after that separte it in ‘|’ to get each value. Now we have all the data

in photo, photo_title and photo_description array. Use the array values as you want…. 🙂

Note: In the data there should not be ‘@@’ or ‘|’. If it contains then you have to choose the separator carefully. Choose a separator which is not come as data.

Have fun with flash + php + MySql….. 😉

Preventing Duplicate Record Insertion on Page Refresh

Most of beginners (including me) face these problems inserting data into database.

When clicking submit button after filluping a form if you refresh the form it will insert the duplicate data. There are two solutions that I know from Google search. May be you can find more by Google search.

And since you cannot avoid a refresh of the screen…

<form action=”insert.php” method=”POST”>
<input type=”text” name=”name”>
<input type=”text” name=”telephone”>
<input type=”text” name=”email”>
<input type=”sumbit” name=”submit”>
</form>

1) If you are doing a form post, you might consider sending a location
header AFTER you inserted the record:

insert.php
// after inserting the data
header(“Location: thanks.php“);
exit();
// do not forget the exit, since your script will run on without it.

In that way your script will process the posting, and then redirects the
browser to thanks.php. This page will contain a message (Thank You). A reload of thanks.php will not result in a fresh db insert.

2) If you’re using sessions, generate serial and supply it as hidden field with
every posting form. Increase it at every post and decline to store data if
serial in the _POST isn’t equal to the one stored in _SESSION.

Combining it with redirects (always good to have such protection), you’ll
probably never got the duplicated submits.