除非重新启动浏览器,否则在更新数据库后不会刷新FLEX数据库

时间:2011-08-31 15:31:52

标签: php mysql database flex refresh

编辑 - 我更新了mxml代码,只显示程序运行更新所需的代码。

我是Flex的新手并且有一个问题我希望你们都可以帮助我。

我已经全面研究了这个问题,大多数人说在更新或添加新记录后使用.refresh()方法更新数据网格。这没有任何作用。

所以,我的问题是这个。我有一个托管在000webhost.com上的mySQL数据库,我通过PHP连接,我有1个mxml文件和2个php文件(我知道我只能使用1但我正在学习,这对我来说更容易用于故障排除)。问题是无论我在datagrid上更改什么,更新都不会在datagrid上显示,除非我关闭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("&amp;","&gt;","&lt;");            //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文件用于插入和其他功能,但不需要它们来解决这个问题。

1 个答案:

答案 0 :(得分:1)

你似乎混淆了差异是同步和异步代码。从你的代码中获取这些行

        updateDatabase.send(objSend);
        xmlFromDatabase.send();
        doSend();
        datalist.refresh();

首先调用updateDatabase.send()。这会对您的服务发出远程请求 然后它调用xmlFromDatabase.send()这也会向您的服务发出远程请求。

这两个都是异步调用。因此,在返回updateDatabase调用的结果之前,将触发xmlFromDatabase调用。

然后你调用dataList.refresh();这实际上什么也没做,因为dataList还没有改变。

然后在某些时候,您的服务将返回。无法保证服务返回值的顺序。更新数据库结果处理程序将只显示警报。 xmlFromDatabase结果处理程序将更新dataList。

我猜你的xmlFromDatabase处理程序是在updateDatabase处理程序之前返回的,因此datagrid永远不会使用新的/更新的数据进行更新,因为它永远不会获得新的或更新的数据。

xmlFromDatabase.send()调用移至updateDatabase结果处理程序,我怀疑您会看到不同的结果。