我创建了一个php表单,用于将值插入数据库。我的表单选项之一是用另一个表中的字段填充的动态列表。我首先创建了没有动态选项的表单,所有数据都插入得很好(现在仍然如此)。现在我尝试包含下面的代码,虽然它正确地显示了选项值,但该值无法插入。有什么建议吗?
<?php
/*
* LIST ALL CATEGORIES
****************************************/
include('../dbconnection.php');
$query = 'SELECT category_id, category_name FROM ingredient_categories';
$result = mysql_query($query);
echo '<select>';
while ($ingredientCategoryOption = mysql_fetch_array($result)) {
echo '<option value="'.$ingredientCategoryOption[category_id].'">'.$ingredientCategoryOption[category_name].'</option>';
}
echo '</select>';
?>
我昨天也创建了类似的东西。$polls
数组在$this->load->view('poll.php', $data['polls'])
中传递给CodeIgniter
中的视图,而您在页面本身中执行此操作。然而,你可以有大致的想法。
<FORM id="formPoll" class="question" name="createpoll" action="<?php echo base_url()?>index.php/poll/selectOption/" method="POST">
<select name="poll_list">
<?php
foreach($polls as $poll){
echo "<option name='poll_table'>$poll->name</option>";
}
?>
</select>
<div id="input">
Poll name: <input type="text" name="name"></input>
Title: <input type="text" name="title"></input>
</div>
<div id="options">
Option: <input type="text" name="option"></input>
</div>
<input type="submit"></input>
</FORM>
一些想法:
- 使用前检查
$results
是否为空 - 给你的
<select>
表格起个名字,就像我上面展示的那样 - 检查
$ingredientCategoryOption
是否不是null
或是返回的内容 - 检查数据库连接