simple insert into MYSQL from Flash via PHP
Copy link to clipboard
Copied
Hi There,
I have spent many days tearing my hair out trying to get this simple flash code to work.
I have four text fields named player,tournament,position,prize and a submit button.
I have a working mysql database. The php file without the POST variables (i.e. using static data) works fine.
I just cannot get the flash to talk to the php correctly. I have looked at many examples and tutorials and forums, but am at a loss.
I have had many flash errors over the days. At the moment it is ArgumentError: Error #1063: Argument count mismatch on actions::main/processLogin(). Expected 0, got 1
Any help would be amazing!
[CODE]<?php
$player = $_POST['player'];
$tournament = $_POST['tournament'];
$position = $_POST['position'];
$prize = $_POST['prize'];
mysql_connect("localhost", "root","poker");
mysql_select_db("poker");
mysql_query("INSERT INTO results(player,tournament,position,prize) VALUES
('$player','$tournamnet','$position','$prize')");
?>[/CODE]
package actions {
import flash.display.MovieClip;
import flash.events.*;
import flash.net.*;
import flash.text.*;
public class main extends MovieClip {
public function main ():void {
submit_button.buttonMode = true;
submit_button.addEventListener(MouseEvent.MOUSE_DOWN, processLogin);
tournament.text = "";
player.text = "";
position.text = "";
prize.text = "";
}
public function processLogin ():void {
var phpVars:URLVariables = new URLVariables();
var phpFileRequest:URLRequest = new URLRequest("php/controlpanel.php");
phpFileRequest.method = URLRequestMethod.POST;
phpFileRequest.data = phpVars;
var phpLoader:URLLoader = new URLLoader();
phpLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
phpVars.player = player.text;
phpVars.tournament = tournament.text;
phpVars.position = position.text;
phpVars.prize = prize.text;
phpLoader.load(phpFileRequest);
}
}
}
Copy link to clipboard
Copied
Hi Dakky,
I suppose the function "processLogin" responds to a mouseclick thus expecting an event as parameter.
If I'm right, changing the line with the function signature to:
public function processLogin( event:MouseEvent ):void
should fix it.
hope it helps,
Manno
Copy link to clipboard
Copied
Thank you sir, gets rid of that error, but still does not make any addition to my database! 😕
Copy link to clipboard
Copied
sometimes hard to debug yes.
You can get the response of the server by listening for the COMPLETE event of the URLLoader. When it is triggered, the response of the server is in it's data property. OTTOMH:
...
phpLoader.addEventListener( Event.COMPLETE, onLoaded );
}
private function onLoaded( event:Event ):void
{
trace ( "response from server: ", event.target.data );
}
Have the PHP output some sensible info on all it does and have server output it's errors so that everything that happens at the server is shown in the output.
Hope this helps,
Manno
Copy link to clipboard
Copied
I really appreciate your help. Yes indeed, debugging and seeing what is going on in general is most difficult with flash!
Once I get this cracked I should be able to complete a lot of what I am trying to achieve.
After inserting some echo's into my php as such:
<?php
$player = $_POST['player'];
$tournament = $_POST['tournament'];
$position = $_POST['position'];
$prize = $_POST['prize'];
mysql_connect("localhost", "root","poker");
echo "onnected to database"
mysql_select_db("poker");
echo "database connected";
mysql_query("INSERT INTO results(player,tournament,position,prize) VALUES
('$player','$tournamnet','$position','$prize')");
echo "result added";
?>
The returned info is a load of nonsense to me:
response from server: %3C%3Fphp%0A%24player%20=%20%24%5FPOST%5B%27player%27%5D%3B%0A%24tournament%20%3D%20%24%5FPOST%5B%27tournament%27%5D%3B%0A%24position%20%3D%20%24%5FPOST%5B%27position%27%5D%3B%0A%24prize%20%3D%20%24%5FPOST%5B%27prize%27%5D%3B%0A%0Amysql%5Fconnect%28%22localhost%22%2C%20%22root%22%2C%22poker%22%29%3B%0Aecho%20%22onnected%20to%20database%22%0Amysql%5Fselect%5Fdb%28%22poker%22%29%3B%0Aecho%20%22database%20connected%22%3B%0Amysql%5Fquery%28%22INSERT%20INTO%20results%28player%2Ctournament%2Cposition%2Cprize%29%20VALUES%20%0A%28%27%24player%27%2C%27%24tournamnet%27%2C%27%24position%27%2C%27%24prize%27%29%22%29%3B%0Aecho%20%22result%20added%22%3B%0A%3F%3E
Copy link to clipboard
Copied
var dt:String = "%3C%3Fphp%0A%24player%20=%20%24%5FPOST%5B%27player%27%5D%3B%0A%24tour nament%20%3D%20%24%5FPOST%5B%27tournament%27%5D%3B%0A%24position%20%3D %20%24%5FPOST%5B%27position%27%5D%3B%0A%24prize%20%3D%20%24%5FPOST%5B% 27prize%27%5D%3B%0A%0Amysql%5Fconnect%28%22localhost%22%2C%20%22root%2 2%2C%22poker%22%29%3B%0Aecho%20%22onnected%20to%20database%22%0Amysql% 5Fselect%5Fdb%28%22poker%22%29%3B%0Aecho%20%22database%20connected%22% 3B%0Amysql%5Fquery%28%22INSERT%20INTO%20results%28player%2Ctournament% 2Cposition%2Cprize%29%20VALUES%20%0A%28%27%24player%27%2C%27%24tournam net%27%2C%27%24position%27%2C%27%24prize%27%29%22%29%3B%0Aecho%20%22re sult%20added%22%3B%0A%3F%3E"
trace( unescape( dt ) );
results in:
<?php
$player = $_POST['player'];
$tour nament = $_POST['tournament'];
$position = $_POST['position'];
$prize = $_POST[% 27prize'];
mysql_connect("localhost", "root%2 2,"poker");
echo "onnected to database"
mysql% 5Fselect_db("poker");
echo "database connected"% 3B
mysql_query("INSERT INTO results(player,tournament% 2Cposition,prize) VALUES
('$player','$tournam net','$position','$prize')");
echo "re sult added";
?>
Copy link to clipboard
Copied
Well my webserver definitely has php running and working
Copy link to clipboard
Copied
Well there must be something wrong since a server would never send actual php code which can, and in your case does contain sensitive information like usernames and passwords.
Copy link to clipboard
Copied
Oh, wait... You're testing from the Flash IDE, right? You are requesting the file not from the server, but from the filesystem. Have the URL read http://... etc. and you're OK (I guess)
Copy link to clipboard
Copied
Thanks Manno, that has got rid of all that trash,
I have an echo at the end of the php, and it is getting that far but not inserting the data to the database. What could I be doing wrong?
It was suggested to me to add some more debugging listeners and I get this results. Any Idea?
openHandler: [Event type="open" bubbles=false cancelable=false eventPhase=2]
progressHandler loaded:13 total: 13
httpStatusHandler: [HTTPStatusEvent type="httpStatus" bubbles=false cancelable=false eventPhase=2 status=200 responseURL=null]
response from server: result=theEnd
result=theEnd
Copy link to clipboard
Copied
Ouput the query too in the PHP. I'm not sure about PHP and it's configs, but it may be something fails silently and you'll never know about it. Use constructs like
mysql_query("...")
or die( "..." )
to get as much out of the error reporting as possible.
Copy link to clipboard
Copied
meh can't get that to work.
I'd like to say I give up but I can't!

Copy link to clipboard
Copied
When you connect to the database you need to store the connection in a variable for use in your query - like so:
$connection = mysql_connect("localhost", "root","poker");
And then use that in query:
mysql_query("INSERT INTO results(player,tournament,position,prize) VALUES ('$player','$tournamnet','$position','$prize')", $connection);
Copy link to clipboard
Copied
Hello there dmennenoh, welcome to my world of pain!
Have done what you said and still nothing updating.
openHandler: [Event type="open" bubbles=false cancelable=false eventPhase=2]
progressHandler loaded:13 total: 13
httpStatusHandler: [HTTPStatusEvent type="httpStatus" bubbles=false cancelable=false eventPhase=2 status=200 responseURL=null]
response from server: result=theEnd

Copy link to clipboard
Copied
Then I would suggest changing your $_POST's to $_REQUEST's and testing via the query string - and tracing your SQL... something like:
<?php
$player = $_REQUEST['player'];
$tournament = $_REQUEST['tournament'];
$position = $_REQUEST['position'];
$prize = $_REQUEST['prize'];
$connection = mysql_connect("localhost", "root","poker");
mysql_select_db("poker");
$sql = "INSERT INTO results(player,tournament,position,prize) VALUES ('$player','$tournamnet','$position','$prize')"
mysql_query($sql, $connection);
echo($sql);
?>
and then test in browser like:
www.mydomain.com/myscript.php?player=pete&tournament=xxx&position=yyy&prize=zzz
Copy link to clipboard
Copied
No luck, just returns
"The website encountered an error while retrieving http://127.0.0.1/reqtest.php?player=9&tournament=9&position=9&prize=9. It may be down for maintenance or configured incorrectly."
for code
<?php
$player = $_REQUEST['player'];
$tournament = $_REQUEST['tournament'];
$position = $_REQUEST['position'];
$prize = $_REQUEST['prize'];
$connection = mysql_connect("localhost", "root","poker");
mysql_select_db("poker");
$sql = "INSERT INTO results(player,tournament,position,prize) VALUES ('$player','$tournamnet','$position','$prize')"
mysql_query($sql, $connection);
echo($sql);
?>

Copy link to clipboard
Copied
You are missing a semicolon at the end of your $sql = line, which PHP requires... but I suspect that's not what's causing that error. I've never seen that error before... you might ask your host about it. Can you even get a very simple php script to work properly? Something like:
<?PHP
echo("Hello World");
?>
Copy link to clipboard
Copied
you won't believe it but it was because I hadn't published it and run it through the browser instead of the IDE. Rookie mistake, but I didn't know about certain security stuff
So much more to make work now but I just wanted to clear it up!!!
Thanks for the help, I'm sure I'll be back

