将多值选择列表框值添加到php中的sql数据库

时间:2014-05-31 15:00:54

标签: php sql forms

我正在尝试从表单帖子向我的SQL表(program_celebrity)添加多个值,我在我的表单中发布以下内容,读取另一个表格如下:

<label for="ProgramListing"></label>
    <select name="ProgramListing" style="width: 800px;" size="10" multiple id="ProgramListing">
      <?php
            $sql_program_listing = "SELECT * FROM program order by ProgramName ASC"; 
            foreach($conn->query($sql_program_listing) as $row_program_listing){ ?>
      <option value="<?=$row_program_listing["ProgramCode"];?>" <?php if ($row_programlisting["id_program"] == $registrant['id_program']) echo 'selected="selected"';?>><?=$row_program_listing["ProgramName"];?> (<?=$row_program_listing["ReleaseDate"] .")";?>
        </option>
      <?php } ?>
      </select>

什么工作正常 现在我想添加以下信息:

$id_celebrity = $_POST['id_celebrity'];
$id_program_celebrity = $_POST['id_celebrity']+$_POST['ProgramListing'];

我使用:

foreach ($_POST['ProgramListing'] as $id_program)
{
$sql_connect_celebrity = "INSERT INTO 
        program_celebrity 
        (
        id_program,
        id_celebrity,
        id_program_celebrity
        )
        VALUES 
        (?,?,?)";

$stmt = $conn->prepare($sql_connect_celebrity);
$stmt->bindValue(1, $id_program);
$stmt->bindValue(2, $id_celebrity);
$stmt->bindValue(3, $id_program_celebrity);
$stmt->execute();
}

但是当我发布一个选项时,它工作正常,但我希望表单发布多个选项并在表中注册它们。似乎没有做对,任何想法我做错了什么?

1 个答案:

答案 0 :(得分:0)

更改表单

<select name="ProgramListing" style="width: 800px;" size="10" multiple id="ProgramListing">

<select name="ProgramListing[]" style="width: 800px;" size="10" multiple id="ProgramListing">