php - 从数据库填充下拉列表

标签 php mysql database forms

我正在尝试使用数据库中的数据在表单上填充我的下拉列表,我尝试了以下但没有成功,它给我一个错误提示 unexpected ;我已尝试删除但仍然出现错误?

 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 文件,一个是 Controller ,我的代码如下:

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();

最佳答案

您的标记中有一些错误,让我们看看:

在这一行中你有一些错误:

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 变量,无论如何我不知道你使用的是哪种框架,所以我只注意到语法错误。

关于php - 从数据库填充下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20639654/

相关文章:

php - MySQL关系循环

php - Cakephp 3 查找所有且仅每个的最大日期

mysql - Adventure Works 用于 SQL 查询

java - 如何纠正这个查询?

java - 如何检查该值是否已存在于android sqlite数据库中?

ios - Realm ,-[__NSCFString timeIntervalSince1970] : unrecognized selector sent to instance

php - 通过连接优化代码

php - 为什么当我相继有两个 mysqli 查询时,我只能执行第一个查询?

mysql - 有多个 MAX 值

mysql - 谁能帮我写这个脚本