2013-12-17 41 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设置为主键,自动增量且不为空。

相关问题