MySQL データベースがあり、PHP スクリプトを介してアクセスします。私が抱えている問題は、PHP から返される変数が常に同じであることです。初めてコードを実行すると (データベースのレコードを追加/削除)、すべてが正しいのですが、再度実行すると (追加/削除)、PHP は以前と同じレコードを AS3 に報告します。ブラウザで直接 php を実行すると、正しい結果が返されます。誰かが私を助けてくれることを願っています...私は夢中になっています!ここに私のコードがあります:
AS3
function sendSqlData(event:Event):void
{
playlistDateString = "&playlistDateString="+playlistDate.getFullYear().toString()+monthDigit+playlistDate.getMonth().toString()+dayDigit+playlistDate.getDate().toString();
playlistSongNr = "&playlistSongNr="+ song;
songTime ="&songTime="+ Math.floor(channel.position);
var phpUrl:String = "send_data.php";
var phpUrlRequest:URLRequest = new URLRequest(phpUrl+parseMe+playlistDateString+playlistSongNr+songTime);
scriptLoader = new URLLoader();
scriptLoader.addEventListener(Event.COMPLETE, sendSqlDataComplete);
scriptLoader.load(phpUrlRequest);
}
function getSqlData(event:Event):void
{
var phpUrl:String = "get_data.php";
var phpUrlRequest:URLRequest = new URLRequest(phpUrl+parseMe);
// phpUrlRequest.method = URLRequestMethod.POST;
scriptLoader = new URLLoader();
// scriptLoader.dataFormat = URLLoaderDataFormat.TEXT;
scriptLoader.addEventListener(Event.COMPLETE, getSqlDataComplete);
scriptLoader.load(phpUrlRequest);
}
function sendSqlDataComplete(event:Event):void
{
scriptLoader.removeEventListener(Event.COMPLETE, sendSqlDataComplete);
var phpVars:URLVariables = new URLVariables();
phpVars.decode(event.target.data);
getSqlData(event);
}
function getSqlDataComplete(event:Event):void
{
scriptLoader.removeEventListener(Event.COMPLETE, getSqlDataComplete);
var phpVars:URLVariables = new URLVariables();
phpVars.decode(event.target.data);
var phpString = event.target.data.toString();
var patterns:Array = ["id\\d{0,5}=\\d{0,5}", "playlistDate\\d{0,5}=\\d{0,8}", "playlistSongNr\\d{0,5}=\\d", "songTime\\d{0,5}=\\d{0,7}"];
for (var j:int = 0; j < 4; j++)
{
var pattern:RegExp = new RegExp(patterns[j],"g");
var tempStrings:Array = phpString.match(pattern);
for (i = 0; i < Number(phpVars.records); i++)
{
switch (j)
{
case 0:
ids[i] = Number(tempStrings[i].substring( tempStrings[i].indexOf("=")+1 ));
break;
case 1:
playlistDates[i] = Number(tempStrings[i].substring( tempStrings[i].indexOf("=")+1 ));
break;
case 2:
playlistSongNrs[i] = Number(tempStrings[i].substring( tempStrings[i].indexOf("=")+1 ));
break;
case 3:
songTimes[i] = Number(tempStrings[i].substring( tempStrings[i].indexOf("=")+1 ));
break;
}
}
tempStrings = null;
}
fillDataGrid(Number(phpVars.records));
}
PHP: send_data.php
<?php
include_once ('connect.php');
$playlistDateString = trim($_GET['playlistDateString']);
$playlistSongNr = trim($_GET['playlistSongNr']);
$songTime = trim($_GET['songTime']);
if($connection)
{
$status .= ("connect=ok&");
//Select database
mysql_select_db($dbname, $connection);
$sql="INSERT INTO $tablename1 (ID, playlistDate, playlistSongNr, songTime, comment) VALUES ('', '$playlistDateString', '$playlistSongNr', '$songTime', 'comment');";
$status .= ("sql=".$sql."&");
// Execute query
if (mysql_query($sql,$connection) )
{
$status .= ("query=ok");
}
else
{
$status .= ("query=error:".mysql_error());
}
}
else
{
$status = ("connect=error: ".mysql_error());
}
echo $status;
mysql_close($connection);
?>
PHP get_data.php
<?php
include_once ('connect.php');
if($connection)
{
$status .= ("connect=ok");
//Select database
mysql_select_db($dbname, $connection);
//Execute query
$query = mysql_query("SELECT * FROM $tablename1");
if ($query)
{
$result = "records=".(mysql_num_rows($query));
$i = 0;
while ($row = mysql_fetch_array($query))
{
$result .= "&id".$i."=".($row["ID"]);
$result .= "&playlistDate".$i."=".($row["playlistDate"]);
$result .= "&playlistSongNr".$i."=".($row["playlistSongNr"]);
$result .= "&songTime".$i."=".($row["songTime"]);
$result .= "&comment".$i."='".($row["comment"])."'";
$i++;
}
$status .= ("&receive=ok");
echo $result."&";
}
else
{
$status .= ("&receive=error");
}
}
else
{
$status .= ("connect=error:".mysql_error());
}
echo $status;
mysql_close($connection);
?>