php从数据库表中选择多个选项

时间:2015-04-24 07:08:36

标签: php mysql

我正在使用以下内容,并希望能够在数据库中进行多次选择和插入。

<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>

3 个答案:

答案 0 :(得分:0)

首先像这样更改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'];?>

你可以用你的数据库做任何你想做的事情

答案 1 :(得分:0)

尝试

<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>

答案 2 :(得分:0)

请按照示例:

<?php
print_r($_POST);
?>
<form action="" method="post">
<select name="accred[]" multiple> 
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
</select>
<input type="submit" value="submit" />
</form>