2011-04-07 34 views
0

我有一個PHP站點連接到Interbase DB。 DB包含用戶可以加載並顯示在屏幕上的訂單。用戶可以更改訂單並保存它們。這可以工作,但如果2個用戶加載並保存相同的記錄,那麼該訂單包含最後一個保存的用戶所做的更改。如何在PHP中使用Interbase事務?

當第二位用戶試圖保存時,我想彈出一條消息,說明訂單已更改並停止保存訂單。

我知道interbase有交易來做到這一點,因爲我有一個桌面應用程序來實現交易和上述場景。但是,我不知道如何在Web環境中對PHP做同樣的事情。

桌面應用程序始終保持數據庫處於打開狀態,事務從讀取到提交時保持活動狀態。使用PHP時,只有在運行每個查詢時纔打開/創建數據庫和事務。從我讀的內容來看,如果事務沒有提交,那麼腳本的末尾會回滾事務。

代碼加載順序

PHP代碼:

public function GetOrderDetails($in_OrderID) 
{ 

    $qry = "SELECT ID, ... , FROM CUSTOMER_INVOICE WHERE ID = $in_OrderID";  

    $this->dbconn = ibase_connect ($this->host, $this->username, $this->password); 
    $this->dbtrans = ibase_trans(IBASE_DEFAULT,$this->dbconn);  
    $result = ibase_query ($this->dbtrans, $qry); 

    while($row = ibase_fetch_row($qryResult)) 
    { 
    } 
    ibase_free_result($in_FreeQry);  
    ibase_close($this->dbconn); 
} 

代碼爲了節省

PHP代碼:

public function SaveOrderDetails() 
{ 
    $DoCommit = false; 
    try 
    {  
     $this->dbconn = ibase_connect ($this->host, $this->username, $this->password); 
     $this->dbtrans = ibase_trans(IBASE_DEFAULT,$this->dbconn);  

     // Insert/Update the order 
     if($this->UpdateOrder()) 
     { 
     // Insert Order Items 
     if($this->InsertOrderItems()) 
     {    
      $DoCommit = true; 
     } 
     else 
     { 
      $this->ErrorMsg = "ERROR 0003: Order Items could not be inserted";       
     }  
     } 
     else 
     { 
      $this->ErrorMsg = "ERROR 0002: Order could not be inserted/updated";    
     } 


     if($DoCommit)   
     { 
     if(ibase_commit($this->dbtrans)) 
     { 
      $OrderResult = true; 
     } 
     else 
     { 
      ibase_rollback($this->dbtrans);   
      $this->ErrorMsg = "ERROR 0004: DB Qry Commit Error";  
      print $this->ErrorMsg ; 
     }   
     } 
     else 
     { 
     ibase_rollback($this->dbtrans); 
     } 
    } 
    catch(Exception $e) 
    { 
     ibase_rollback($this->dbtrans); 
     $this->ErrorMsg = "ERROR 0001: DB Exception: " . $e;  
    }   
    ibase_close($this->dbconn);  
} 

如果有人能告訴我,我要去哪裏錯了那太好了。或者,如果沒有人使用Interbase,你會如何使用MySQL?我不想去表鎖,時間戳路線。

感謝 雷

回答

0

必須使用主鍵,以避免它。你可以使用生成器來獲取每個訂單的唯一ID。