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

USE*_*RR5 1 database actionscript-3 flash-cs6

我想将我的得分/ myname值从我的fla文件保存到数据库使用as3通过php.i在Xampp中创建了一个名为admin的数据库和一个表得分(有一列名称).我有1个as3文件两个php文件到连接数据库和发送变量,我采取了一个变量myname我想从as3发送到我的数据库表所有是完美的但表不更新任何帮助将是非常有帮助
这里是我的代码

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';
?>
Run Code Online (Sandbox Code Playgroud)

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();
?>
Run Code Online (Sandbox Code Playgroud)

这是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;
}
Run Code Online (Sandbox Code Playgroud)

我只是想要当我点击提交按钮时,我的数据库表将被更新并将数据保存到表中.我已经尝试手动添加数据.但我想使用输入text.i进行输入web.i进行了网络搜索,as3数据库连接到处创建问题:(

akm*_*ozo 5

首先,您应该知道MySQL扩展在PHP 5.5.0之前已被弃用,并且将来会被删除,这就是为什么您应该使用其他扩展,如MySQLiPDO_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

?>
Run Code Online (Sandbox Code Playgroud)

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

<?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);

?>
Run Code Online (Sandbox Code Playgroud)

你的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
}
Run Code Online (Sandbox Code Playgroud)

希望能有所帮助.