我正在使用以下内容,希望能够在数据库中进行多项选择和插入。
<select name="accred">
<option value="0">-- Please Select --</option>
<?
$result = mysql_query( "SELECT * FROM stock_livestock_accred ORDER BY name ASC" );
while( $row = mysql_fetch_object( $result ) ) { ?>
<option value="<?= $row->id ?>"<?= $this->row->accred == $row->id ? " selected" : "" ?>><?= $row->name ?></option>
<? } ?>
</select>
最佳答案
首先像这样更改您的HTML
<select name="accred[]" multiple>
<option value="0">-- Please Select --</option>
<?
$result = mysql_query("SELECT * FROM stock_livestock_accred ORDER BY name ASC");
while( $row = mysql_fetch_object($result)) { ?>
<option value="<?= $row->id ?>"<?= $this->row->accred == $row->id ? " selected" : "" ?>><?= $row->name ?></option>
<? } ?>
</select>
然后获取select
元素的值,如下所示
<?php $accred=$_POST['accred'];?>
你可以用你的数据库做任何你想做的事情
关于php从数据库表中选择多个选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29840909/