PDA

View Full Version : Inserting data into a database using Flash



mprzybylski
November 27th, 2004, 12:35 AM
I have a little application with five text fields and a submit button that i want to put the inputted data from into a database. i have my database done and everything, and here is the code for the PHP file i'm using:


<?php
$server = "localhost";
$username = "reintro_matt";
$password = "*******";
$database = "reintro_testdb";
$table = "ecard";

mysql_pconnect($server, $username, $password);
mysql_select_db($database);

$success = 1;

$ins_str = "INSERT INTO " . $table . " VALUES ('".$HTTP_POST_VARS['fromName']."', '".$HTTP_POST_VARS['fromEmail']."', '".$HTTP_POST_VARS['toName']."', '".$HTTP_POST_VARS['toEmail']."', '".$HTTP_POST_VARS['message']."')";

//echo $ins_str;

if (!mysql_query ($ins_str)) {
$success = 0;
$msg = 'Problem saving records to the database';
} else {
$msg = 'Record was saved successfully. Refreshing display...';
}

echo '&success='.$success.'&msg='.$msg.'&';

?>

i'm not very good with PHP but after doing some research and reading some tutorials i think that is good to go. my text fields in flash have the variables that i'm using in the POST_VARS commands in PHP and similar instance names. how would i go about writing the actionscript to collect this data and insert it into the database in flash (or make corrections to the PHP i already have if it isn't ready).

thanks in advance.

VoS
November 27th, 2004, 03:16 AM
im not sure if the way i do it is the reccomended way but it is easy to do..

simply use a button code like..



button_instancename.onPress = function()
{
ran=Math.random()*3023466666622366628762387623;
myVars = new LoadVars();
myVars.load("sendtome.php?var1="+v1+"&var2="+v2+"&randomizer="+ran,"0");
myVars.onLoad = function() {
//what happens when page has been loaded here
}
}


this is the easiest way ive found to send stuff so far
+"&randomizer="+ran
i have that line so that a person could send the same data twice even if they have the web cache on ( the adress will always be different)

havent tested the script but i think i wrote it right
good luck , VoS
(you can also do it with _post , but im always to lazy to do that
this way you get the variables just as if you were browsing to the page normally thru the browser)

mprzybylski
November 27th, 2004, 02:36 PM
ok, this is the code i had on the button, and it didn't work (as in it didn't actually add the thing to the database, the flash part worked but the addition didnt):


stop();
//this puts the cursor in the first box
Selection.setFocus(fromName_txt);

status_txt.text = "";

submit_btn.onRelease = function()
{
ran=Math.random()*3023466666622366628762387623;
myVars = new LoadVars();
myVars.load("insert.php?from_name="+from_name+"&from_email="+from_email+"&to_name"+to_name+"&to_email"+to_email+"&message"+message+"&randomizer="+ran,"0");
status_txt.text = "Message is being sent...";
myVars.onLoad = function() {
//what happens when page has been loaded here
status_txt.text = "Information sent successfully!";
}
}


and i'm still using the same code for the PHP part that i had pasted above. anyone else?

EDIT: i also have an auto_increment value of msgID set in the database to track unique IDs, does this matter with the code i am writing or can it just be left alone?

Digitalosophy
November 27th, 2004, 03:00 PM
Maybe this will help
http://www.digitalosophy.com/blog/index.php?p=17

mprzybylski
November 27th, 2004, 04:09 PM
Digitalosophy, you my friend, are the MAN!

Digitalosophy
November 27th, 2004, 04:37 PM
lol glad i can help :thumb: