简体   繁体   English

as3,MySQL PHP连接

[英]as3, MySQL PHP connection

am trying to connect my flash project with database. 正在尝试将我的Flash项目与数据库连接。 However, i want to be able to submit and retrieve data from database having 3 fields (name, score and date). 但是,我希望能够从具有3个字段(名称,得分和日期)的数据库中提交和检索数据。 My problem is that if I click submit button and when i go to check in database i only see 0 score meaning nothing is submitted. 我的问题是,如果我单击提交按钮,并且当我去检查数据库时,我只会看到0分,这意味着什么也没有提交。 Can someone help me on this. 有人可以帮我吗 Tq Here is my coding for flash: Tq这是我对flash的编码:

var str:String = "";
var myscore = 0;
btn_submit.addEventListener(MouseEvent.CLICK, submitted);

function submitted(e:MouseEvent)
{
var myrequest:URLRequest = new URLRequest("http://127.0.0.1/Y/sendscore.php");
myrequest.method = URLRequestMethod.POST;
var variables:URLVariables = new URLVariables();
variables.name = str;
variables.score = myscore;
myrequest.data = variables;
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.addEventListener(Event.COMPLETE, dataOnLoad);
loader.load(myrequest);
}

function dataOnLoad(evt:Event)
{
MC_success.alpha=100;
//status is a custom flag passed from back-end 
}

MY PHP CODE FOR SENDING DATA; 我的PHP代码,用于发送数据;

<?php
    //Include database connection details
    require_once('config.php');

        //Connect to mysql server
    $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
    if(!$link) {
        die('Failed to connect to server: ' . mysql_error());
    }

    //Select database
    $db = mysql_select_db(DB_DATABASE);
    if(!$db) {
        die("Unable to select database");
    }

//Function to sanitize values received from the form. Prevents SQL injection
    function clean($str) {
        $str = @trim($str);
        if(get_magic_quotes_gpc()) {
            $str = stripslashes($str);
        }
        return mysql_real_escape_string($str);
    }

    //Sanitize the POST values
    $name = clean($_POST['name']);
    $score = clean($_POST['score']);
        $currentdate = date("Y/m/d");

    //Create INSERT query
    $qry = "INSERT INTO highscores(user, score, date) VALUES('$name','$score','$currentdate')";
    $result = @mysql_query($qry);
echo "writing=Ok";
exit();
mysql_close();  

?>

MY PHP CODE TO RETRIEVE DATA; 我的PHP代码以检索数据;

<?php   
    //Include database connection details
    require_once('config.php');

        //Connect to mysql server
    $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
    if(!$link) {
        die('Failed to connect to server: ' . mysql_error());
    }

    //Select database
    $db = mysql_select_db(DB_DATABASE);
    if(!$db) {
        die("Unable to select database");
    }

    //Create INSERT query
    $qry = "SELECT * FROM highscores ORDER BY score ASC";
    $result = @mysql_query($qry);
        $num=mysql_numrows($result);
if($num > 10)
{$num = 10;}
//echo "writing=Ok";

echo "<b><center>Best Times:</center></b><br /><table>";

$i=0;
$i2=1;
while ($i < $num) {

$name=mysql_result($result,$i,"user");
$time=mysql_result($result,$i,"score");
$date=mysql_result($result,$i,"date");


echo "<tr><td align=left valign=top>$i2.</td><td align=center valign=top><b>$name</b> | $score | $date</td></tr><tr><td colspan=2><hr></td></tr>";
$i2++;
$i++;
}
echo "</table>";
//$urlRefresh = "scores.php";
//header("Refresh: 15; URL=\"" . $urlRefresh . "\"");

exit();
mysql_close();


?>

Unless you've missed out some piece of code whereby the score is changed, you're actually declaring var myscore = 0 right at the top of your AS3 code block. 除非您错过了一些改变分数的代码,否则实际上是在AS3代码块顶部声明var myscore = 0

The first thing would be to change that to 100 , then run you script and see if that modified score variable is getting submitted. 首先是将其更改为100 ,然后运行脚本并查看是否已提交修改过的得分变量。 If it is, then everything is working as it should. 如果是这样,那么一切都会按预期进行。

Update: 更新:

You've changed your question, and you want to be able to load data. 您已经更改了问题,并且希望能够加载数据。 You already have a function set up for this via loader.addEventListener(Event.COMPLETE, dataOnLoad) . 您已经通过loader.addEventListener(Event.COMPLETE, dataOnLoad)为此设置了一个函数。 You just need to grab the data your PHP script is sending back. 您只需要获取PHP脚本发送回的数据。 This can be accessed via your evt parameter in the dataOnLoad function: 可以通过dataOnLoad函数中的evt参数进行访问:

function dataOnLoad(evt:Event):void {   
    trace("Data submission complete");
    var returnVars = evt.target.data;

    trace("***********************");

    for (var myVars in returnVars) {
        trace(myVars + ": " + returnVars[myVars]);
    }

    trace("***********************");
}

Update 2: 更新2:

You've requested help with loading your scores from the database. 您已请求帮助从数据库加载分数。 As you already have a PHP file that retrieves this from the database (let's assume it's called scores.php ),m you just need a function in Flash to load it. 由于您已经有一个PHP文件可以从数据库中检索此文件(假设它称为scores.php ),因此您只需要Flash中的函数即可加载它。

You already have the basic functions in place, making use of URLLoader and Event Listeners. 您已经使用URLLoader和Event Listeners具备了基本功能。 You just need these to apply to a straightforward load: 您只需要将这些应用于简单的负载即可:

btn_scores.addEventListener(MouseEvent.CLICK, loadScores);

function loadScores(e:MouseEvent):void {
    var fileLoader:URLLoader = new URLLoader();
    fileLoader.addEventListener(Event.COMPLETE, scoresLoadComplete);

    fileLoader.load(new URLRequest("scores.php"));
}

function scoresLoadComplete(evt:Event):void {
    try {
        var returnVars = evt.target.data;

        trace("***********************");
        for (var myVars in returnVars) {
            trace(myVars + ": " + returnVars[myVars]);
        }
        trace("***********************");
    } catch (err:Error) {
        trace("Can't parse loaded file: " + err.message);
    }
}

Note that your PHP file currently returns an HTML table of results. 请注意 ,您的PHP文件当前返回一个HTML结果表。 This won't behave in Flash; 这在Flash中不起作用; you'd be much better off sending through key/value pairs and parsing them or just a basic HTML list of scores. 您最好发送键/值对并对其进行解析,或者仅解析分数的基本HTML列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM