如何在分离的选择标记中向数据库中插入两个以上的选项标记值

时间:2016-09-07 14:15:16

标签: php pdo

的index.php

<!DOCTYPE html>
</html>
<body>

<form name="fruits" action="selectexec.php" method="post">
<select name="department">
<option value="apple">apple</option>
<option value="mango">mango</option>
<option value="orange">orange</option>
</select>

<select name="company">
<option value="ASUS">ASUS</option>
<option value="LENOVO">LENOVO</option>
<option value="ACER">ACER</option>
</select>

<input type="submit" name="submit" />
</form>

</body>
</html>

selectexe.php

<?php
include_once('pdo-connect.php');
if(isset($_POST['submit'])){
$department=$_POST['department'];
$company=$_POST['company'];


// SQL statements
$sql = "INSERT INTO selectformtbl (department,company)values('$department',$company)";
$db->exec($sql);
}

?>

1 个答案:

答案 0 :(得分:0)

<select>代码应具有multiple属性。

<select name="department[]" multiple></select>
<select name="company[]" multiple></select>

在PHP中,您可以内插所有这些值并直接存储到数据库。

$departments = implode($_POST['department']);
$company = implode($_POST['company']);
$sql = "INSERT INTO selectformtbl (`department`,`company`) values ('$department','$company')";
$db->exec($sql);