使用 as3 通过 php 在数据库中插入值

using as3 to insert values in database via php

我想通过 php.i 使用 as3 将我的 score/myname 值从我的 fla 文件保存到数据库 已经在 Xampp 中创建了一个名为 admin 的数据库和 table 分数(只有一列名称)。我有 1 个 as3 文件和两个 php 文件来连接数据库并发送变量,我已经使用了一个变量 myname,我想从 as3 全部发送到我的数据库 table很完美,但是 table 没有更新任何帮助都会很有帮助
这是我的代码

connect.php

<?php
$db_name = "admin";
$db_username = "root";
$db_password = "";
$db_host = "localhost";
mysql_connect($db_host, $db_username, $db_password, $db_name);
mysql_select_db($db_name) or die (mysql_error());
echo 'success';
?>

xxx.php

<?php
include("connect.php");
$link = mysql_connect($db_host, $db_username, $db_password);
if(!$link) {
die('Failed to connect to server.'.mysql_error());
}
echo 'Connected successfully';
$db = mysql_select_db($db_name);

if(!$db) 
{
   die("unable to select database");
}
$Name = $_POST['myname'];
mysql_query("INSERT INTO score (Name) VALUES('$Name')") ;
echo 'System Updated'; 
mysql_close();
?>

这是 as3 代码

import flash.display.MovieClip;
import flash.events.*;
import flash.net.*;
import flash.text.*;
import flash.events.MouseEvent;
import flash.net.URLRequest;
import flash.net.URLVariables;
import flash.net.URLLoader;

btn.addEventListener(MouseEvent.MOUSE_DOWN, Givehiscore);

function Givehiscore(event:MouseEvent){

        var myVariables:URLVariables = new URLVariables();
        myVariables.myname = "sarah";

        var myRequest:URLRequest = new URLRequest("xxx.php");
        myRequest.method = URLRequestMethod.POST;
        myRequest.data = myVariables;
        var myLoader:URLLoader = new URLLoader(myRequest);
        myLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
        myLoader.addEventListener(Event.COMPLETE, dataOnLoad);
        myLoader.load(myRequest);
        }
function dataOnLoad(evt:Event) 
{
    success.alpha = 100;
}

我只是希望当我点击提交按钮时,我的数据库 table 将被更新并将数据保存到 table..这里我尝试手动添加数据。但我想使用输入 text.i 进行网络搜索,因为 3 数据库连接到处都是问题:(

首先,你应该知道MySQL extension is deprecated as of PHP 5.5.0, and will be removed in the future, that's why you should use another extension like MySQLi or PDO_MySQL

所以使用 MySQLi,您的连接脚本 (connect.php) 可以是这样的:

<?php

    // connect.php

    $db_name = 'test';
    $db_username = 'root';
    $db_password = '';
    $db_host = 'localhost';

    $link = mysqli_connect($db_host, $db_username, $db_password, $db_name);
    if (mysqli_connect_errno()) {
        die('Failed to connect to the server : '.mysqli_connect_error());
    }

    // avoid to use echo here because it will be sent to ActionScript

?>

将数据发送到您的数据库的脚本可以是这样的:

<?php

    // data.php

    include('connect.php');

    // if we are here, that's mean that we are already connected to mysql server
    // and we don't need to do another connection

    $name = $_POST['myname'];

    // $link is the same connection created in your connect.php script
    if (mysqli_query($link, "INSERT INTO score (Name) VALUES('$name')")) {

        // if you want use "loader.dataFormat = URLLoaderDataFormat.VARIABLES" in your ActionScript code
        // you have to use the echo like this : echo 'var=value';
        echo 'result=System Updated';

        // otherwise, the URLLoader data format will be the default value which is "text"

    } else {
        echo 'result=error';
    }

    mysqli_close($link);

?>

还有你的 ActionScript 代码:

btn.addEventListener(MouseEvent.MOUSE_DOWN, Givehiscore);
function Givehiscore(event:MouseEvent) {

    var variables:URLVariables = new URLVariables();
        variables.myname = 'sarah';         

    var request:URLRequest = new URLRequest('http://localhost/data.php');
        request.method = URLRequestMethod.POST;
        request.data = variables;

    var loader:URLLoader = new URLLoader(request);
        loader.dataFormat = URLLoaderDataFormat.VARIABLES;
        loader.addEventListener(Event.COMPLETE, dataOnLoad);
        loader.load(request);           

}
function dataOnLoad(e:Event) {
    var variables:URLVariables = URLVariables(e.target.data);
    trace(variables.result);    // gives : System Updated
}

希望能帮到你。