2013-12-17 128 views
1

我想填充我的下拉列表與數據庫中的數據形式,我已經嘗試了以下,但沒有成功,它給了我一個錯誤,說意外;我試過刪除但仍然收到錯誤?從數據庫中填充下拉列表

echo form::label('myproduct', 'My Product:'); 
     echo form::select('form[myproduct]', $sql = mysql_query("SELECT description FROM claim_incentive"); while ($row = mysql_fetch_array($sql)) 
      {echo "<option value=\"1\">" . $row['description'] . "</option>"; } 

我想我可以把代碼放錯了地方,因爲我有兩個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` (`User Reference`, `Claimant Postcode`, `Purchase Order No.`, `Claimant Email Address`, `Storename`, `Storetown`, `Date of Sale`, `Date of Delivery`, `Acknowledgement No.`, `Product`) 
                     VALUES (:userreference, :claimantpostcode, :orderno, :email, :storename, :storetown, :dateofsale, :dateofdelivery, :acknowledgementno, :product)'); 
            $stmt->param(':userreference', $post['userreference']); 
            $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(':product', $post['product']); 
             try { 
               $stmt->execute(); 
               $this->template->content->post = $post; 
               $this->template->content->thanks = true; 
               } catch (Exception $e) { 
                FB::error($e); 
               } 

        } 
       } 

,另一個是實際的形式,請參閱下面的一部分:

echo form::label('dateofdelivery', 'Date of Delivery:'); 
     echo form::input('form[dateofdelivery]', $val, array('class'=>'input', 'id'=>'dateofdelivery')); 

     echo form::label('acknowledgementno', 'Acknowledgement No:'); 
     echo form::input('form[acknowledgementno]', $val, array('class'=>'input', 'id'=>'acknowledgementno')); 

     echo form::label('product', 'Product:'); 
     echo form::select('form[product]', array(
              '' => 'Please select from the list', 
              'In store' => 'In store', 
              'Word of mouth' => 'Word of mouth', 
              'Television' => 'Television', 
              'Newspaper' => 'Newspaper', 
              'Magazine' => 'Magazine', 
              'Internet' => 'Internet', 
              'Google Reasearch' => 'Google Reasearch', 
              'Radio' => 'Radio', 
              'Medical Recommendation' => 'Medical Recommendation', 
          ), '', array('class="select"', 'id'=>'product')); 

     echo form::submit('btnSubmit', 'Submit', array('id'=>'btnSubmit', 'class'=>'button')); 
     echo '</div>'; 
     echo '<div class="clearfix"></div>'; 

     echo form::close(); 
+0

嘗試var_dumping你的結果,並確保你實際上有東西要顯示。查詢是否被執行?並使用mysqli或pdo而不是mysql –

回答

0

您的標記一些錯誤,讓我們來看看:

在這一行喲u有一些錯誤:

echo form::select('form[myproduct]', $sql = mysql_query("SELECT description FROM claim_incentive"); 

你打開兩個括號,但只有關閉一個應該是這樣的:

echo form::select('form[myproduct]', $sql = mysql_query("SELECT description FROM claim_incentive")); 

這是沒有必要的SQL變量分配方法中,任何方式我不不知道你在使用哪種框架,所以我只注意到語法錯誤。

+0

我想我可能會把代碼放在錯誤的地方,我有兩個php文件,我會更新我的問題,謝謝! –