更新数据库后,FLEX 数据库不会刷新,除非重新启动浏览器
编辑 - 我已经更新了 mxml 代码,仅显示程序运行更新所需的代码。
我是 Flex 新手,有一个问题,希望大家能帮助我解决。
我已经研究了这个问题,大多数人说在更新或添加新记录后使用 .refresh() 方法来更新数据网格。这没有任何作用。
所以,我的问题是这样的。我有一个托管在 000webhost.com 上的 mySQL 数据库,我通过 PHP 连接,我有 1 个 mxml 文件和 2 个 php 文件(我知道我只能使用 1 个,但我正在学习,这对我来说更容易用于故障排除)。问题是无论我在数据网格上更改什么,更新都不会显示在数据网格上,除非我关闭 IE 并重新打开它。我可以刷新或在另一个窗口中打开页面,但更新不显示。我必须完全退出并重新启动才能显示任何更新或添加内容。
这是我的代码。我希望有人能提供帮助。
这是我的 licenseTracker.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
initialize="doSend()"
>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.controls.List;
import mx.controls.Text;
import mx.events.CloseEvent;
import mx.events.ListEvent;
import mx.rpc.events.ResultEvent;
//Create array collection to hold data for datagrid
[Bindable]
private var datalist:ArrayCollection = new ArrayCollection;
private var objSend:Object = new Object;
//call hosted data php
public function doSend():void {
xmlFromDatabase.send();
}
//function for updating existing records
public function doUpdate():void {
objSend.updateAssetId = dgAssetId.text;
objSend.updateProgramData = dgProgram.text;
objSend.updateSerialData = dgSerial.text;
objSend.updateUserData = dgUser.text;
objSend.updateCostData = dgCost.text;
objSend.updateStatusData = dgStatus.text;
updateDatabase.send(objSend);
xmlFromDatabase.send();
doSend();
datalist.refresh();
}
protected function xmlFromDatabase_resultHandler(event:ResultEvent):void
{
datalist = event.result.data.row;
}
private function alertUpdateClickHandler(eventObj:CloseEvent):void
{
if(eventObj.detail==Alert.YES)
doUpdate();
else
{
//Do Nothing
}
}
private function itemClickEvent(event:ListEvent):void
{
//apply selected datagrid to string to textbox
dgAssetId.text=String(event.currentTarget.selectedItem.asset_id);
dgProgram.text=String(event.currentTarget.selectedItem.program);
dgSerial.text=String(event.currentTarget.selectedItem.serial);
dgUser.text=String(event.currentTarget.selectedItem.user_id);
dgCost.text=String(event.currentTarget.selectedItem.cost);
dgStatus.text=String(event.currentTarget.selectedItem.status_id);
}
]]>
</fx:Script>
<fx:Declarations>
<s:HTTPService url="http://mypage.com/data.php"
id="xmlFromDatabase"
showBusyCursor="true"
result="xmlFromDatabase_resultHandler(event)"
method="POST" />
<s:HTTPService url="http://mypage/update.php"
id="updateDatabase"
showBusyCursor="true"
method="POST"
resultFormat="text"
result='Alert.show("Record Successfully Updated. Record May Take A Minute To Update")'
/>
</fx:Declarations>
<s:VGroup paddingLeft="20" y="10" width="1000" horizontalAlign="center">
<s:Label width="670" height="32" color="#FF0000" fontFamily="Georgia" fontSize="24"
text="Software License Tracker " textAlign="center"/>
<!--Add Data grid-->
<mx:DataGrid id="dg" x="10" y="79" width="1000" dataProvider="{datalist}"
editable="false" itemClick="itemClickEvent(event)">
<mx:columns>
<mx:DataGridColumn headerText="Asset ID" dataField="asset_id" width="60"/>
<mx:DataGridColumn headerText="Program" dataField="program" width="200"/>
<mx:DataGridColumn headerText="Serial" dataField="serial" width="270"/>
<mx:DataGridColumn headerText="User" dataField="user" width="125"/>
<mx:DataGridColumn headerText="Cost" dataField="cost" width="50"/>
<mx:DataGridColumn headerText="Status" dataField="description" width="190" />
</mx:columns>
</mx:DataGrid>
</s:VGroup>
<!--Panel for adding new entries into datagrid-->
<s:Panel id="addFrame" x="10" y="263" width="443" height="210"
textAlign="left" title="Add/Edit Record">
<s:Label x="60" y="10" width="191" id="dgAssetId" />
<s:TextInput x="60" y="30" width="191" id="dgProgram" />
<s:TextInput x="60" y="58" width="191" id="dgSerial"/>
<s:TextInput x="60" y="84" width="191" id="dgUser" />
<s:TextInput x="60" y="112" width="191" id="dgCost"/>
<s:TextInput id="dgStatus" x="60" y="142" width="191"/>
<s:Label x="6" y="10" text="Asset ID"/>
<s:Label x="6" y="35" text="Program"/>
<s:Label x="6" y="62" text="Serial"/>
<s:Label x="6" y="89" text="User"/>
<s:Label x="6" y="117" text="Cost"/>
<s:Label x="6" y="147" text="Status"/>
<s:Button id="changeSubmit" x="312" y="33" label="Update" click='Alert.show("Are you sure you want to update the selected record?", "Update Record", 3, this, alertUpdateClickHandler);'/>
</s:Panel>
</s:Application>
我的 data.php 文件,它从服务器请求数据:
<?php
//SQL Connection Info - update with your database, username & password
$host = "hostsite";
$username = "username";
$password = "password";
$db_name = "database";
$mysql_connection = mysql_connect($host, $username, $password);
mysql_select_db($db_name);
//Change this query as you wish for single or multiple records
$result = mysql_query("SELECT a.asset_id, a.program, a.serial, u.user_id, concat(u.first_name ,' ', u.last_name) as user, a.cost, s.description, s.status_id
FROM assets a
JOIN status s ON a.status = s.status_id
JOIN users u ON a.user_id = u.user_id
order by 1
");
//Get the number of rows
$num_row = mysql_num_rows($result);
//Start the output of XML
echo '<?xml version="1.0" encoding="iso-8859-1"?>';
echo "<data>";
echo '<num>' .$num_row. '</num>';
if (!$result) {
die('Query failed: ' . mysql_error());
}
/* get column metadata - column name -------------------------------------------------*/
$i = 0;
while ($i < mysql_num_fields($result)) {
$meta = mysql_fetch_field($result, $i);
$ColumnNames[] = $meta->name; //place col name into array
$i++;
}
$specialchar = array("&",">","<"); //special characters
$specialcharReplace = array("&",">","<"); //replacement
/* query & convert table data and column names to xml ---------------------------*/
$w = 0;
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<row>";
foreach ($line as $col_value){
echo '<'.$ColumnNames[$w].'>';
$col_value_strip = str_replace($specialchar, $specialcharReplace, $col_value);
echo $col_value_strip;
echo '</'.$ColumnNames[$w].'>';
if($w == ($i - 1)) { $w = 0; }
else { $w++; }
}
echo "</row>";
}
if($num_row == "1"){
echo '<row></row>';
}
echo "</data>";
mysql_free_result($result);
mysql_close();
?>
这是我的 update.php
//SQL Connection Info - update with your database, username & password
$host = "hostsite";
$username = "username";
$password = "password";
$db_name = "database";
$mysql_connection = mysql_connect($host, $username, $password);
mysql_select_db($db_name);
$id = $_POST["updateAssetId"];
$program = $_POST["updateProgramData"];
$serial = $_POST["updateSerialData"];
$user = $_POST["updateUserData"];
$cost = $_POST["updateCostData"];
$status = $_POST["updateStatusData"];
$query = "UPDATE `database`.`assets` SET
`program` = '$program',
`serial` = '$serial',
`user_id` = '$user',
`cost` = '$cost',
`status` = '$status'
WHERE `asset_id` = '$id'";
if ( !mysql_query($query, $mysql_connection) ){
die('ERROR: '. mysql_error() );
}
/* echo "id: " . $id;
echo "Program: " . $program;
echo "Serial: " . $serial;
echo "User: " . $user;
echo "Cost: " . $cost;
echo "Status: " . $status; */
echo "UPDATE SUCCESSFUL, 1 Record Updated";
mysql_close();
?>
我还有其他用于插入和其他功能的 php 文件,但解决此问题不需要它们。
EDIT - I have updated the mxml code to only show the needed code for the program to run the update.
I am new to Flex and have an issue I hope you all can help me out with.
I have researched this issue all over and most people say use the .refresh() method to update a datagrid after updating or adding a new record. This does not do anything.
So, my issue is this. I have a mySQL database hosted on 000webhost.com, I am connecting via PHP, I have 1 mxml file and 2 php files (I know I can use just 1 but I am learning and this was easier for me to use for troubleshooting). The issue is no matter what I change on the datagrid the update does not show on the datagrid unless I close out of IE and reopen it. I can refresh or open the page in another window and the update does not show. I have to exit entirely and restart in order for any updates or additions to show.
Here is my code. I hope someone can help.
Here is my licenseTracker.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
initialize="doSend()"
>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.controls.List;
import mx.controls.Text;
import mx.events.CloseEvent;
import mx.events.ListEvent;
import mx.rpc.events.ResultEvent;
//Create array collection to hold data for datagrid
[Bindable]
private var datalist:ArrayCollection = new ArrayCollection;
private var objSend:Object = new Object;
//call hosted data php
public function doSend():void {
xmlFromDatabase.send();
}
//function for updating existing records
public function doUpdate():void {
objSend.updateAssetId = dgAssetId.text;
objSend.updateProgramData = dgProgram.text;
objSend.updateSerialData = dgSerial.text;
objSend.updateUserData = dgUser.text;
objSend.updateCostData = dgCost.text;
objSend.updateStatusData = dgStatus.text;
updateDatabase.send(objSend);
xmlFromDatabase.send();
doSend();
datalist.refresh();
}
protected function xmlFromDatabase_resultHandler(event:ResultEvent):void
{
datalist = event.result.data.row;
}
private function alertUpdateClickHandler(eventObj:CloseEvent):void
{
if(eventObj.detail==Alert.YES)
doUpdate();
else
{
//Do Nothing
}
}
private function itemClickEvent(event:ListEvent):void
{
//apply selected datagrid to string to textbox
dgAssetId.text=String(event.currentTarget.selectedItem.asset_id);
dgProgram.text=String(event.currentTarget.selectedItem.program);
dgSerial.text=String(event.currentTarget.selectedItem.serial);
dgUser.text=String(event.currentTarget.selectedItem.user_id);
dgCost.text=String(event.currentTarget.selectedItem.cost);
dgStatus.text=String(event.currentTarget.selectedItem.status_id);
}
]]>
</fx:Script>
<fx:Declarations>
<s:HTTPService url="http://mypage.com/data.php"
id="xmlFromDatabase"
showBusyCursor="true"
result="xmlFromDatabase_resultHandler(event)"
method="POST" />
<s:HTTPService url="http://mypage/update.php"
id="updateDatabase"
showBusyCursor="true"
method="POST"
resultFormat="text"
result='Alert.show("Record Successfully Updated. Record May Take A Minute To Update")'
/>
</fx:Declarations>
<s:VGroup paddingLeft="20" y="10" width="1000" horizontalAlign="center">
<s:Label width="670" height="32" color="#FF0000" fontFamily="Georgia" fontSize="24"
text="Software License Tracker " textAlign="center"/>
<!--Add Data grid-->
<mx:DataGrid id="dg" x="10" y="79" width="1000" dataProvider="{datalist}"
editable="false" itemClick="itemClickEvent(event)">
<mx:columns>
<mx:DataGridColumn headerText="Asset ID" dataField="asset_id" width="60"/>
<mx:DataGridColumn headerText="Program" dataField="program" width="200"/>
<mx:DataGridColumn headerText="Serial" dataField="serial" width="270"/>
<mx:DataGridColumn headerText="User" dataField="user" width="125"/>
<mx:DataGridColumn headerText="Cost" dataField="cost" width="50"/>
<mx:DataGridColumn headerText="Status" dataField="description" width="190" />
</mx:columns>
</mx:DataGrid>
</s:VGroup>
<!--Panel for adding new entries into datagrid-->
<s:Panel id="addFrame" x="10" y="263" width="443" height="210"
textAlign="left" title="Add/Edit Record">
<s:Label x="60" y="10" width="191" id="dgAssetId" />
<s:TextInput x="60" y="30" width="191" id="dgProgram" />
<s:TextInput x="60" y="58" width="191" id="dgSerial"/>
<s:TextInput x="60" y="84" width="191" id="dgUser" />
<s:TextInput x="60" y="112" width="191" id="dgCost"/>
<s:TextInput id="dgStatus" x="60" y="142" width="191"/>
<s:Label x="6" y="10" text="Asset ID"/>
<s:Label x="6" y="35" text="Program"/>
<s:Label x="6" y="62" text="Serial"/>
<s:Label x="6" y="89" text="User"/>
<s:Label x="6" y="117" text="Cost"/>
<s:Label x="6" y="147" text="Status"/>
<s:Button id="changeSubmit" x="312" y="33" label="Update" click='Alert.show("Are you sure you want to update the selected record?", "Update Record", 3, this, alertUpdateClickHandler);'/>
</s:Panel>
</s:Application>
my data.php file that requests data from the server:
<?php
//SQL Connection Info - update with your database, username & password
$host = "hostsite";
$username = "username";
$password = "password";
$db_name = "database";
$mysql_connection = mysql_connect($host, $username, $password);
mysql_select_db($db_name);
//Change this query as you wish for single or multiple records
$result = mysql_query("SELECT a.asset_id, a.program, a.serial, u.user_id, concat(u.first_name ,' ', u.last_name) as user, a.cost, s.description, s.status_id
FROM assets a
JOIN status s ON a.status = s.status_id
JOIN users u ON a.user_id = u.user_id
order by 1
");
//Get the number of rows
$num_row = mysql_num_rows($result);
//Start the output of XML
echo '<?xml version="1.0" encoding="iso-8859-1"?>';
echo "<data>";
echo '<num>' .$num_row. '</num>';
if (!$result) {
die('Query failed: ' . mysql_error());
}
/* get column metadata - column name -------------------------------------------------*/
$i = 0;
while ($i < mysql_num_fields($result)) {
$meta = mysql_fetch_field($result, $i);
$ColumnNames[] = $meta->name; //place col name into array
$i++;
}
$specialchar = array("&",">","<"); //special characters
$specialcharReplace = array("&",">","<"); //replacement
/* query & convert table data and column names to xml ---------------------------*/
$w = 0;
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<row>";
foreach ($line as $col_value){
echo '<'.$ColumnNames[$w].'>';
$col_value_strip = str_replace($specialchar, $specialcharReplace, $col_value);
echo $col_value_strip;
echo '</'.$ColumnNames[$w].'>';
if($w == ($i - 1)) { $w = 0; }
else { $w++; }
}
echo "</row>";
}
if($num_row == "1"){
echo '<row></row>';
}
echo "</data>";
mysql_free_result($result);
mysql_close();
?>
here is my update.php
//SQL Connection Info - update with your database, username & password
$host = "hostsite";
$username = "username";
$password = "password";
$db_name = "database";
$mysql_connection = mysql_connect($host, $username, $password);
mysql_select_db($db_name);
$id = $_POST["updateAssetId"];
$program = $_POST["updateProgramData"];
$serial = $_POST["updateSerialData"];
$user = $_POST["updateUserData"];
$cost = $_POST["updateCostData"];
$status = $_POST["updateStatusData"];
$query = "UPDATE `database`.`assets` SET
`program` = '$program',
`serial` = '$serial',
`user_id` = '$user',
`cost` = '$cost',
`status` = '$status'
WHERE `asset_id` = '$id'";
if ( !mysql_query($query, $mysql_connection) ){
die('ERROR: '. mysql_error() );
}
/* echo "id: " . $id;
echo "Program: " . $program;
echo "Serial: " . $serial;
echo "User: " . $user;
echo "Cost: " . $cost;
echo "Status: " . $status; */
echo "UPDATE SUCCESSFUL, 1 Record Updated";
mysql_close();
?>
I have other php files for inserts and other functions but they should not be needed to fix this issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您似乎混淆了同步和异步代码的差异。从您的代码中获取这些行
首先,它调用 updateDatabase.send()。这会向您的服务发出远程请求。
然后它调用 xmlFromDatabase.send() 这也向您的服务发出远程请求。
这两个都是异步调用。因此,在返回 updateDatabase 调用的结果之前,会触发 xmlFromDatabase 调用。
然后你调用 dataList.refresh();它本质上什么也没做,因为 dataList 尚未更改。
然后在某个时候,您的服务将会恢复。无法保证服务返回值的顺序。更新数据库结果处理程序将仅显示警报。 xmlFromDatabase 结果处理程序将更新 dataList。
我猜想您的 xmlFromDatabase 处理程序是在 updateDatabase 处理程序之前返回的,因此数据网格永远不会使用新的/更新的数据进行更新,因为它永远不会获取新的或更新的数据。
将 xmlFromDatabase.send() 调用移至 updateDatabase 结果处理程序,我怀疑您会看到不同的结果。
You seem to be confusing the differences be synchronous and Asynchronous code. Take, these lines, from your code
First it calls updateDatabase.send(). This makes a remote request on your service.
Then it calls xmlFromDatabase.send() This also makes a remote request to your service.
Both of these are asynchronous calls. So you the xmlFromDatabase call is triggered before the results of the updateDatabase call are returned.
Then you call dataList.refresh(); which in essence does nothing because the dataList has not been changed yet.
Then at some point, your services will return back. There is no way to guarantee the order that the services will return values. The update database result handler will just show an alert. The xmlFromDatabase result handler will update the dataList.
I would guess that your xmlFromDatabase handler is being returned before the updateDatabase handler, therefore the datagrid never updates with new/updated data because it never gets new or updated data.
Move the
xmlFromDatabase.send()
call to the updateDatabase result handler, and I suspect you'll see different results.