PDA

View Full Version : Sending flash variables to MySQL database via php _POST


DrGonzo1208
03-05-2008, 03:35 PM
Hi there,
I've got a flash application that holds a large amount of variabes that I want to add to the database. I currently have it sending data via sendAndLoad (POST) to a php file that then writes the variables to a txt file. i.e:

Flash code
var myVars:LoadVars = new LoadVars();
myVars.onLoad = function():Void {
if (myVars.verify == "success") {
status_txt.text = "save successful";
} else {
status_txt.text = "save failed";
}
};
saveButton.onPress = function() {
myVars.productName = productName_txt.text;
myVars.productCode = productCode_txt.text;
myVars.productColour = productColour_txt.text;
myVars.productGroup = productGroup_txt.text;
myVars.sizeRange = sizeRange_txt.text;
myVars.colourRange = colourRange_txt.text;
myVars.sendAndLoad("writeToFile.php", myVars, "POST");
}


php script
<?php
$productName = $_POST['productName'];
$productCode = $_POST['productCode'];
$productColour = $_POST['productColour'];
$productGroup = $_POST['productGroup'];
$sizeRange = $_POST['sizeRange'];
$colourRange = $_POST['colourRange'];
$designName = $_POST['designName'];

$filename = $designName;

$data = "&productName=".$productName."&\n"
."&productCode=".$productCode."&\n"
."&productColour=".$productColour."&\n"
."&productGroup=".$productGroup."&\n"
."&sizeRange=".$sizeRange."&\n"
."&colourRange=".$colourRange."&";

$open = fopen($filename.'.txt', 'a');
$write = fwrite($open, $data);

if($write)
{
echo "&verify=success&";
}
else
{
echo "&verify=fail&";
}
?>

The above code works just fine for writing the data to a txt file, but ultimately I'd like to find a way to send these variables to a MySQL database. At the moment i'm just trying to echo or print the _POST array as a test. I can't even echo one variable from the _POST data (i.e echo "$productCode";...

If anyone can see what might be wrong - I would be most greatful...

Thanks.

makoinater
03-06-2008, 07:21 PM
Try this thread
http://www.actionscript.org/forums/showthread.php3?t=138998

it has everything even a demo zip file, its how I learnt to do it. Although it presumes you have a MySql Database setup with correct tables. all you hav to change is the host ip username and password on the php file and adjust the database selection.

Kevvan
03-30-2008, 07:42 AM
<?php

$con = mysql_connect("localhost","root","yourpw");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("your_db", $con);


mysql_close($con);
?>
<html>
<body>

<form action="sova.php" method="post">
productName: <input type="text" name="productName" />
<br>
productCode: <input type="text" name="productCode" />
<br>
productColour: <input type="text" name="productColour" />
<br>
productGroup: <input type="text" name="productGroup" />
<br>
sizeRange: <input type="text" name="sizeRange" />
<br>
colourRange: <input type="text" name="colourRange" />
<br>
designName: <input type="text" name="designName" />
<input type="submit" />
</form>

</body>
</html>
<?php
$con = mysql_connect("localhost","root","yourpw");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("your_db", $con);

$sql="INSERT INTO product (productName, productCode, productColour, productGroup, sizeRange, colourRang, designName)
VALUES
('$_POST[productName]','$_POST[productCode]','$_POST[productColour]','$_POST[productGroup]','$_POST[sizeRange]','$_POST[colourRang]','$_POST[designName]')";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "added!";

mysql_close($con)
?>
<?php
$filename = $designName;

$data = "&productName=".$productName."&\n"
."&productCode=".$productCode."&\n"
."&productColour=".$productColour."&\n"
."&productGroup=".$productGroup."&\n"
."&sizeRange=".$sizeRange."&\n"
."&colourRange=".$colourRange."&";

$open = fopen($filename.'.txt', 'a');
$write = fwrite($open, $data);

if($write)
{
echo "&verify=success&";
}
else
{
echo "&verify=fail&";
}
?>

flash_fest
04-08-2008, 01:19 PM
The link posted above is not available..it sayz page not found.. so kindly help me out i m also facing the same problem but i m totally new to php and my sql..So anybody.plz HELP ! :) Ill b really greatful..Thnkx.

Paul Ferrie
04-08-2008, 01:59 PM
The link posted above is not available..it sayz page not found.. so kindly help me out i m also facing the same problem but i m totally new to php and my sql..So anybody.plz HELP ! :) Ill b really greatful..Thnkx.
Link seems to be working now

1kat
05-03-2008, 06:23 AM
DrGonzo1208,
I have a flash application online and I am trying to figure out how to save to an external .txt file on the hosting server. I am having trouble finding detailed documentation on this. The code you posted at the begining of this thread looks like it will do the trick, but I was hoping that you could please comment it for me. I want to fully understand the code instead of just copying it. I would greatly appreciate your help. Thx.

1kat
05-05-2008, 08:36 AM
Can anybody inform me if the code that DrGonzo1208 first posted works on Flash MX 6.0?