Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

Storing data in DataBase with Flash CS3

This is a discussion on Storing data in DataBase with Flash CS3 within the Tutorials forums, part of the Flash English category; Often happens that we are developing a website with Adobe Flash to a client and / or friend and it's ...


Go Back   Forum Flash CS3 Flash CS4 > Flash CS3 Flash CS4 > Flash English > Tutorials

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 27-08-08, 06:15
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
Storing data in DataBase with Flash CS3

Often happens that we are developing a website with Adobe Flash to a client and / or friend and it's required to store the data of users via a form.
How to do?
This tutorial will try to explain to less experienced how to enter data into a database ( mySQL) by Flash.

First we need to reaffirm the notion that Flash and mySQL can not communicate directly.
Need a server-side scripts (in this case PHP) that receives data from Flash and then insert them in the database.

I've created an example using a form in Flash that sends data to a PHP script (name, first name, address, city, state, email) which in turn will store them in the database.

First of all we must create a new table in our DB: in this case a table called "clients" with 7 columns (ID, name, surname, address, city, state, email).

The file. Sql to create the necessary table is as follows:
PHP Code:
CREATE TABLE `clients` (
`
IDINT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`
nameTEXT NOT NULL ,
`
surnameTEXT NOT NULL ,
`
addressTEXT NOT NULL ,
`
cityTEXT NOT NULL ,
`
stateTEXT NOT NULL ,
`
emailTEXT NOT NULL
ENGINE MYISAM 
I create a form in Flash as follows:
-- Six static text fields that contain the titles (name, surname, address, city, state, email)
-- Six input text fields in which the user will insert his informations
-- A dynamic text field that will display any messages for user (for example if it has not completed all fields)
-- A button for sending data

Here it is:





The Document Class ( Main.as ) contains the following code:
Code:
/*
 *************************************
 * Data Store Flash CS3 + PHP  
 * http://www.FlepStudio.org         
 * Author: Filippo Lughi           
 * version 1.0                       
 *************************************
 */
package org.FlepStudio
{
	import flash.display.*;
	import flash.events.*;
	import flash.text.*;
	import flash.utils.*;
	import flash.net.*;
	import caurina.transitions.Tweener;
	
	public class Main extends MovieClip
	{
		private var email_checker:CheckEmail;
		
		private var timer:Timer;
		
		private var film_mc:MovieClip;
		
		public static var _Name:String;
		public static var _Surname:String;
		public static var _Address:String;
		public static var _City:String;
		public static var _State:String;
		public static var _Email:String;
		
		public function Main()
		{
			addEventListener(Event.ADDED_TO_STAGE,init);
		}
		
		private function init(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE,init);
			
			form_mc.x=stage.stageWidth+10;
			
			stage.frameRate=31;
			
			email_checker=new CheckEmail();
			
			moveInForm();
			addButtonListener();
		}
		
		private function moveInForm():void
		{
			Tweener.addTween(form_mc,{x:stage.stageWidth/2-form_mc.width/2,time:0.7,transition:"easeOutElastic"});
		}
		
		private function addButtonListener():void
		{
			form_mc.send_btn.addEventListener(MouseEvent.MOUSE_DOWN,checkFields);
		}
		
		private function checkFields(evt:MouseEvent):void
		{
			if(email_checker.initCheck(form_mc.email_txt.text))
			{
				if(form_mc.name_txt.text!=''&&form_mc.surname_txt.text!=''&&form_mc.address_txt.text!=''&&form_mc.city_txt.text!=''&&form_mc.state_txt.text!='')
				{
					showWorking();
					_Name=form_mc.name_txt.text;
					_Surname=form_mc.surname_txt.text;
					_Address=form_mc.address_txt.text;
					_City=form_mc.city_txt.text;
					_State=form_mc.state_txt.text;
					_Email=form_mc.email_txt.text;
					
					var send_data:StoreData=new StoreData(this);
				}
				else
					displayError("fill in all the fields please");
			}
			else
				displayError("invalid e-mail");
		}
		
		private function displayError(s:String):void
		{
			form_mc.error_txt.text=s;
			hideError();
		}
		
		private function hideError():void
		{
			timer=new Timer(2500,1);
			timer.addEventListener(TimerEvent.TIMER,deleteError);
			timer.start();
		}
		
		private function deleteError(evt:TimerEvent):void
		{
			form_mc.error_txt.text='';
		}
		
		private function showWorking():void
		{
			film_mc=new MovieClip();
			film_mc.graphics.beginFill(0xFFFFFF,0.7);
			film_mc.graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
			addChild(film_mc);
		}
		
		public function hideWorking():void
		{
			removeChild(film_mc);
			
			displayError("Data has been stored");
			
			reset();
		}
		
		private function reset():void
		{
			form_mc.name_txt.text='';
			form_mc.surname_txt.text='';
			form_mc.address_txt.text='';
			form_mc.city_txt.text='';
			form_mc.state_txt.text='';
			form_mc.email_txt.text='';
			
			form_mc.x=stage.stageWidth+10;
			
			moveInForm();
		}
	}
}
At the following line:
var send_data:StoreData=new StoreData(this);
I instance a new class with name StoreData, this class will send data to PHP.
It's the following code:
Code:
/*
 *************************************
 * Data Store Flash CS3 + PHP  
 * http://www.FlepStudio.org         
 * Author: Filippo Lughi           
 * version 1.0                       
 *************************************
 */
package org.FlepStudio
{
	import flash.events.*;
	import flash.net.*;
	import flash.display.MovieClip;
	
	public class StoreData
	{
		private var request:URLRequest=new URLRequest('http://www.flepstudio.org/swf/ImmagazzinareDati/storeData.php');
		
		private var _fla:MovieClip;
		
		public function StoreData(fla:MovieClip)
		{
			_fla=fla;
			
			sendToPHP();
		}
		
		private function sendToPHP():void
		{
			var variables:URLVariables=new URLVariables();
			variables.name=Main._Name;
			variables.surname=Main._Surname;
			variables.address=Main._Address;
			variables.city=Main._City;
			variables.state=Main._State;
			variables.email=Main._Email;
			request.method=URLRequestMethod.POST;
			request.data=variables;
			var loader:URLLoader=new URLLoader();
			loader.dataFormat=URLLoaderDataFormat.VARIABLES;
			addListeners(loader);
			trace(variables);
			try 
			{
				loader.load(request);
			} 
			catch (error:Error) 
			{
				trace('Unable to load requested document.');
			}
		}
		
		private function addListeners(d:IEventDispatcher):void
		{
			d.addEventListener(Event.COMPLETE,onComplete);
		}
		
		private function onComplete(e:Event):void
		{
			var loader:URLLoader=URLLoader(e.target);
			var vars:URLVariables=new URLVariables(loader.data);
			if(vars.answer=="ok")
			{
				_fla.hideWorking();
			}
		}
	}
}
The PHP file:
PHP Code:
<?php
    
/*
 *************************************
  Flash CS3 Data Store Form                   
  http://www.flepstudio.org     
  Author: Filippo Lughi          
  version 1.0                                
 *************************************
 */
 /******** CHANGE YOUR DATABASE SETTING *******/
    
$dbhost 'localhost'// database host ( usually localhost )
    
$dbuser 'root'// database username
    
$dbpass 'root'// database password
    
$dbname 'name'// database name
    
$mysql mysql_connect($dbhost,$dbuser,$dbpass);
    
mysql_select_db($dbname);
    
    
/* VARIABLES FROM FLASH */
    
$name=$_REQUEST["name"];
    
$surname=$_REQUEST["surname"];
    
$address=$_REQUEST["address"];
    
$city=$_REQUEST["city"];
    
$state=$_REQUEST["state"];
    
$email=$_REQUEST["email"];
    
    
/*INSERT INTO DB*/
    
$Query "INSERT INTO `".$dbname."`.`clients` (`ID`, `name`, `surname`, `address`, `city`, `state`, `email`) VALUES (NULL, \"".$name."\", \"".$surname."\",\"".$address."\",\"".$city."\",\"".$state."\",\"".$email."\");";
    
    
/* ECHO TO FLASH */
    
if(mysql_query($Query))
    {
        
$answer='ok';
        echo 
"answer=".$answer;
    }
    else
    {
        
$answer='nope';
        echo 
"answer=".$answer;
    }
?>
Next time we'll see how to retrieve the data.
EDIT: here is the second part: Retrieve data from mySQL with Flash CS3
Source files:
Attached Files
File Type: zip StoringData.zip (75.2 KB, 285 views)

__________________

 


I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !

Last edited by Flep; 14-10-08 at 18:22..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #2 (permalink)  
Old 18-09-08, 06:07
Junior Member
 
Join Date: Jul 2007
Posts: 27
Rep Power: 0
Zombie is on a distinguished road
Re: Storing data in DataBase with Flash CS3

I keep getting

1119: Access of possibly undefined property _Name through a reference with static type Class.

can you explain what that might mean?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 10-10-08, 13:42
Junior Member
 
Join Date: Oct 2008
Posts: 2
Rep Power: 0
markerpen is on a distinguished road
Re: Storing data in DataBase with Flash CS3

Sweet - very impressed

Been looking at a way to do this. be nice to upload images and their pathname ( which can be used in a dynamic xml file ) as well using something similar to this that will fit in :-
$file=$_FILES['userfile']['name'];
$uploaddir = '/your/folder/';
$basefile = basename($file);
$uploadfile = $uploaddir . basename($file);
move_uploaded_file($_FILES['userfile']['images']);

$sql="INSERT INTO table ('filename') VALUES ('$file')";
?>

Looking forward to see how the data is extracted....Been getting headaches trying to turn fields in the database fields into E4X xml element with attributes...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 22-12-08, 07:04
Junior Member
 
Join Date: Dec 2008
Posts: 2
Rep Power: 0
koalapig is on a distinguished road
Re: Storing data in DataBase with Flash CS3

how do i d/l the files?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 26-12-08, 13:25
Onsitus's Avatar
CSS.FlepStudio.org
 
Join Date: Jul 2007
Location: Nettuno Beach
Posts: 1,060
Rep Power: 3
Onsitus is on a distinguished road
Re: Storing data in DataBase with Flash CS3

Quote:
Originally Posted by koalapig View Post
how do i d/l the files?
See attached files at the end of the first post by Flep!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Reply

Bookmarks

Tags
DataBase, mysql, php

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads

Thread Thread Starter Forum Replies Last Post
Retrieve data from mySQL with Flash CS3 Flep Tutorials 3 23-11-08 16:42
Actionscript 2 Gestione Database AS2 andrea_u_beddu Flash Italiano 2 05-11-08 08:07
Inserire dati nel DataBase da Flash CS3 Flep Articoli e tutorials 1 14-10-08 18:23
Flex manages the back-end and Flash displays the data Flep Flex builder 3 ENG 3 02-08-08 09:50
put complex data from php to flash nootropic.kint Actionscript 3.0 avanzato 3 15-10-07 22:52


All times are GMT. The time now is 15:32.

Powered by vBulletin version 3.7.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC4
Forum SiteMap