我知道这个问题经常出现,但我没有在互联网上的每个教程中找到答案。我不知道那里有什么错误,是用Flash连接flash还是用MySql连接php。 这是我的代码。
main.as
package actions {
/*
always extend a class using movieclip instead of sprite when using flash.
*/
import flash.display.MovieClip;
import flash.events.*;
import flash.net.*;
import flash.text.*;
/*
create our class
*/
public class main extends MovieClip {
public function main ():void {
/*
buttonMode gives the submit button a rollover
*/
submit_button.buttonMode = true;
/*
what this says is that when our button is pressed, the checkLogin function will run
*/
submit_button.addEventListener(MouseEvent.MOUSE_DOWN, checkLogin);
/*
set the initial textfield values
*/
username.text = "";
pass.text = "";
}
/*
the function to check login
*/
public function checkLogin (e:MouseEvent):void {
/*
check fields before sending request to php
*/
if (username.text == "" || pass.text == "") {
/*
if username or password fields are empty set error messages
*/
if (username.text == "") {
username.text = "Enter your username";
}
if (pass.text == "") {
pass.text = "Enter your password";
}
} else {
/*
init function to process login
*/
processLogin();
}
}
/*
function to process our login
*/
public function processLogin ():void {
/*
variables that we send to the php file
*/
var phpVars:URLVariables = new URLVariables();
/*
we create a URLRequest variable. This gets the php file path.
*/
var phpFileRequest:URLRequest = new URLRequest("php/controlpanel.php");
/*
this allows us to use the post function in php
*/
phpFileRequest.method = URLRequestMethod.POST;
/*
attach the php variables to the URLRequest
*/
phpFileRequest.data = phpVars;
/*
create a new loader to load and send our urlrequest
*/
var phpLoader:URLLoader = new URLLoader();
phpLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
phpLoader.addEventListener(Event.COMPLETE, showResult);
/*
now lets create the variables to send to the php file
*/
phpVars.systemCall = "checkLogin";
phpVars.username = username.text;
phpVars.pass = pass.text;
/*
this will start the communication between flash and php
*/
phpLoader.load(phpFileRequest);
}
/*
function to show the result of the login
*/
public function showResult (event:Event):void {
/*
this autosizes the text field
***** You will need to import flash's text classes. You can do this by:
import flash.text.*;
*/
result_text.autoSize = TextFieldAutoSize.LEFT;
/*
this gets the output and displays it in the result text field
*/
var resultVar:URLLoader = new URLLoader(event.target.data.systemResult);
result_text.text = ""+resultVar;
}
}
}
controlpanel.php
<?php
/*
connect to our database
*/
include_once "connect.php";
/*
we post the variables we recieve from flash
*/
$username = $_POST['username'];
$pass = $_POST['pass'];
/*
if flash called the login system the code below runs
*/
if ($_POST['systemCall'] == "checkLogin") {
/*
The * means the query initally selects all the records in the database.
Then the query filters out any records that are not the same as what the user entered.
*/
$sql = "SELECT * FROM users WHERE username='$username' AND password='$pass'";
$query = mysql_query($sql);
/*
This counts how many records match our query
*/
$login_counter = mysql_num_rows($query);
/*
if $login_counter is greater we send a message back to flash and get the data from the database
*/
if ($login_counter > 0) {
/*
we use a while loop to get the user's bio. mysql_fetch_array gets all the field's data for the particular record.
*/
while ($data = mysql_fetch_array($query)) {
/*
gets the user_bio value from the selected record
*/
$userbio = $data["user_bio"];
/*
use the print function to send values back to flash
*/
print "systemResult=$userbio";
}
} else {
print "systemResult=The login details dont match our records.";
}
}
?>
connect.php
<?php
/*
Database vars that we use to connect to our mysql database. Change the values to your database settings.
*/
$database = "admin";
$username = "root";
$password = "";
$host = "localhost";
/*
mysql_connect is a built in function that allows us to make an easy connection.
*/
mysql_connect($host, $username, $password, $database);
/*
mysql_select_db is a built in function that allows us to select the database. This is an essential function.
We use the 'die' function to check for errors.
*/
mysql_select_db($database) or die (mysql_error());
echo 'success';
?>
在我成为数据库之前。
数据库
请帮助我找错。我将非常感激,并感谢您的回答。提前谢谢。答案 0 :(得分:0)
您需要在服务器端测试您的PHP / MySQL连接(只需编写一个带有静态值的PHP脚本,看看您是否会收到错误)。对于客户端,尝试将这些事件添加到您的加载器,它可能会给您一个错误的提示:
phpLoader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
phpLoader.addEventListener(IOErrorEvent.NETWORK_ERROR, onIOError);
phpLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
phpLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, onHTTPStatus);
private function onIOError(e:IOErrorEvent):void
{
trace("onIOError " + e.toString());
}
private function onSecurityError(e:SecurityErrorEvent):void
{
trace("onSecurityError " + e.toString());
}
private function onHTTPStatus(e:HTTPStatusEvent):void
{
trace("onHTTPStatus statuscode " + e.status + " - " + e.toString());
}