2013-12-17 42 views
0

我有一個表單將它捕獲的數據發送到數據庫,我有一個主鍵連接到我的表(form_id),我想每次自動增加一個新表單時提交併因此添加到我的數據庫表中。目前它只是增加一個爲提交的第一個表格然後我不提交表格,因爲它給了我一個消息,說你不能有兩個相同的id爲零,這是正確的,所以我想改變這個?自動增量MySQL中的主鍵當從表格中接收到數據時

下面是我的PHP代碼,將數據提交到數據庫:

public function action_claimincentive() { 
        $this->template->content = View::factory('crm/uk/claim_incentive_form'); 
        $this->template->content->thanks = false; 
        $this->template->content->val = ''; 
        $this->template->content->post = ''; 

         if ($this->request->post('form')) { 
            $post = $this->request->post('form'); 

            $stmt = DB::query(Database::INSERT, 'INSERT INTO `claim_incentive_form_data` (`Claimant Name`, `Claimant Postcode`, `Purchase Order No.`, `Claimant Email Address`, `Storename`, `Storetown`, `Date of Sale`, `Date of Delivery`, `Tempur Acknowledgement No.`, `Tempur Product`) 
                     VALUES (:claimantname, :claimantpostcode, :orderno, :email, :storename, :storetown, :dateofsale, :dateofdelivery, :acknowledgementno, :tempurproduct)'); 
            $stmt->param(':claimantname', $post['claimantname']); 
            $stmt->param(':claimantpostcode', $post['claimantpostcode']); 
            $stmt->param(':orderno', $post['orderno']); 
            $stmt->param(':email', $post['email']); 
            $stmt->param(':storename', $post['storename']); 
            $stmt->param(':storetown', $post['storetown']); 
            $stmt->param(':dateofsale', $post['dateofsale']); 
            $stmt->param(':dateofdelivery', $post['dateofdelivery']); 
            $stmt->param(':acknowledgementno', $post['acknowledgementno']); 
            $stmt->param(':tempurproduct', $post['tempurproduct']); 
             try { 
               $stmt->execute(); 
               $this->template->content->post = $post; 
               $this->template->content->thanks = true; 
               } catch (Exception $e) { 
                FB::error($e); 
               } 

        } 
       } 

回答

2

這聽起來更像是一個MySQL的問題,而不是別的。確保您的主鍵設置爲自動增量。嘗試更改表格以添加自動增量功能:

ALTER TABLE [table name] MODIFY COLUMN [column name] [column type] PRIMARY KEY AUTO_INCREMENT; 

在上例中。將括號中的鍵替換爲適當的名稱。將[表名]替換爲表的名稱,[列名]用列的名稱和[列類型]替換爲列的類型(SMALLINT,INT等)。

欲瞭解更多信息,請參閱this答案發布人roman

有關AUTO_INCREMENT功能的更多信息,請查看MySQL的開發文檔here

0

您必須將form_id設置爲主鍵,自動增量且不爲空。

相關問題