Click to See Complete Forum and Search --> : Updating table from Flash via PHP - PLEASE HELP!
jacobdesch
02-01-2010, 09:04 AM
I'm at my wits' end on this one. As it says, I'm trying to update a SQL table from Flash via PHP. After some other forum posts and help I've received, I've got this code:
$table = "store_data";
/*$quantities = $_POST['quantities'];*/
$link = mysql_connect("localhost", "username", "password");
mysql_select_db("database", $link);
$i = 0;
foreach($_POST['quantities'] as $quantity){
$link = mysql_query("UPDATE $table SET total_emails = $quantity WHERE number = $_POST['counters'][i]");
i++;
}
mysql_close();
And from Flash, I'm sending two variables, "quantities," an array that is populated with the values that need to be updated, and "counters," also an array, which is strictly used for reference, so that only the values that need to be changed will be updated.
But, for some reason, when I try to test it, nothing gets updated and the function doesn't even seem to be running correctly. I've attached a screenshot of what my table looks like. Can anyone please give me some advice and point me in the right direction as to why this isn't working??? Thanks in advance.
svidgen
02-01-2010, 09:40 AM
Try putting $ in front of i, both in your query and in the increment immediately after.
jacobdesch
02-01-2010, 09:53 AM
Funny you should say that. I just got finished doing that...still nothing....
$table = "store_data";
$quantities = $_POST['quantities'];
$link = mysql_connect("localhost", "username", "password");
mysql_select_db("database", $link);
$i = 0;
foreach($_POST['quantities'] as $quantity){
$link = mysql_query("UPDATE $table SET total_emails = $quantity WHERE number = $_POST['counters'][$i]");
$i++;
}
mysql_close();
svidgen
02-01-2010, 10:01 AM
Oh ... also, you shouldn't be assigning the result to $link. Try this:
$table = "store_data";
$quantities = $_POST['quantities'];
$link = mysql_connect("localhost", "username", "password");
mysql_select_db("database", $link);
$i = 0;
foreach($_POST['quantities'] as $quantity){
$result = mysql_query("UPDATE $table SET total_emails = $quantity WHERE number = $_POST['counters'][$i]");
if (!$result) {
print "error: " . mysql_error() . "; ";
}
$i++;
}
mysql_close();
Now, tell us what errors you get (if any).
jacobdesch
02-01-2010, 10:17 AM
Well, therein lies the problem. Since I'm running it through Flash and just externally referencing the PHP file, I don't really have any way to track those kind of errors. But I can trace variables getting passed from the PHP like so:
$i = 0;
foreach($_POST['quantities'] as $quantity){
echo "&arrayFeedback=success";
$result = mysql_query("UPDATE $table SET total_emails = $quantity WHERE number = $_POST['counters'][$i]");
if (!$result) {
print "error: " . mysql_error() . "; ";
}
$i++;
}
and I can tell you that "arrayFeedback" isn't even returning anything, so it doesn't even seem to be getting to that point.
svidgen
02-01-2010, 10:28 AM
You really need to try calling your script from outside of Flash, if possible. Build a form to POST to the script for the purpose of debugging. Assuming you don't have much more processing to do than what you've posted here, you should be able to build a static page fairly quickly.
You just really "need" to be able to see the raw output when you're trying to debug something like this.
jacobdesch
02-01-2010, 12:33 PM
OK, after some work, I've done this:
<form id="form1" name="form1" method="post" action="path_to_form.php">
<select name="quantities[]" size=5 multiple>
<option value="1" SELECTED>1
<option value="2" SELECTED>2
<option value="3" SELECTED>3
<option value="4" SELECTED>4
<option value="5" SELECTED>5
</select>
<p>
<label>
<input type="submit" name="button" id="button" value="Submit" />
</label>
</p>
</form>
I've given up with the whole "counters" fiasco. I've never been able to get it to work and it seems like more trouble than its worth because the client will be updating all the values in the table at once. And still, the script wasn't working and not reporting any errors. The above code works with this:
$table = "store_data";
$quantities = $_POST['quantities'];
$link = mysql_connect("localhost", "username", "password");
mysql_select_db("database", $link);
$i = 0;
foreach($_POST['quantities'] as $quantity){
$result = mysql_query("UPDATE $table SET total_emails = $quantity WHERE number = $i+1");
if (!$result) {
print "error: " . mysql_error() . "; ";
}
$i++;
}
mysql_close();
It works to update the database, which unfortunately means that the problem is with my Flash. Is there a difference between the way Flash and PHP handles arrays? Flash outputs arrays like this:
[1,2,3,4,5]
Do I need to somehow convert them to work with PHP?
criterion9
02-01-2010, 12:59 PM
You are still going to run into problems in the future if there is an anomaly with the PK for your table. The best solution is to only update the rows that are changed as I suggested in the other thread.
How are you building your POST vars in Flash? AS2 or AS3?
jacobdesch
02-01-2010, 02:08 PM
Yes, I'm going to look into that now. Rather than reference everything by "number," I'm going to use the "id" field, since those numbers will never repeat. Now, I'm just working on how to output only the variables that I need to update from Flash and how to reference them in the PHP. I'm using AS2 to post my variables from Flash. Any hints on how to convert AS arrays to PHP arrays?
criterion9
02-01-2010, 03:28 PM
How are you building your POST request in AS2 (i.e. what code are you using to make your request to the server)?
jacobdesch
02-01-2010, 03:38 PM
Here's a slimmed-down version of what I've got going on:
var theXML:XML = new XML();
var quantArray:Array = new Array();
theXML.ignoreWhite = true;
theXML.onLoad = function(success) {
if (success) {
var stores = this.firstChild.childNodes;
for (i=1; i<=stores.length; i++) {
quantArray[i-1] = (parseInt(stores[i-1].childNodes[4].firstChild.nodeValue));
}
} else {
trace("error loading XML");
}
};
function loadXML() {
theXML.load("http://www.mysite.com/stores.php");
}
var dataIn:LoadVars = new LoadVars();
var dataOut:LoadVars = new LoadVars();
dataIn.onLoad = function(ok) {
if (ok) {
loadXML();
}
};
function updateTotal() {
dataOut.quantities = _root.quantArray;
dataOut.sendAndLoad("http://www.mysite.com/update.php",dataIn,"POST");
}
loadXML();
The updateTotal function is called by a button after the fields have been edited.
criterion9
02-01-2010, 06:12 PM
Basically I would create a parseable single string you then explode (http://php.net/explode) with php to turn into an array. It doesn't look like loadVars supports sending/receiving arrays based on the quick searches I did (I'm more of a AS3 guy). I would try to get it working using an html/php version first and then get the flash portion working so you don't have to try to trouble shoot a moving target.
http://snipplr.com/view/8878/as2-loadvars-php-example/
http://board.flashkit.com/board/showthread.php?t=584906
jacobdesch
02-01-2010, 08:08 PM
Funny you should say that. I had just gotten done doing that exact thing. Exporting it as a string and then just exploding it with PHP worked like a charm. Now I just gotta figure out how to parse, sort, graph and update all this information....
Thanks for all your help!