几个下拉列表输入到一个字段表

时间:2013-06-11 02:57:11

标签: php javascript html arrays

任何人都可以帮我解决这个问题,我有几个下拉列表,我想插入表中。但我只需要将它插入一个字段,任何人都有一个很好的想法如何做到这一点:)

这是下拉列表的示例。

<select name="level1" style="width:225px;">
<option value=0 selected></option>
<?php
include 'connect.php';
$q  = "SELECT attribute_id,attribute_name FROM attribute ";
$result = $db->query($q);
while ($row1 = $result->fetch()){
  echo "<option value='$row1[attribute_name]'>$row1[attribute_name]</option>";
}
?>
</select>


<select name="level1" style="width:225px;">
<option value=0 selected></option>
<?php
include 'connect.php';
$q  = "SELECT attribute_id,attribute_name FROM attribute ";
$result = $db->query($q);
while ($row1 = $result->fetch()){
  echo "<option value='$row1[attribute_name]'>$row1[attribute_name]</option>";
}
?>
</select><br>

<select name="level1" style="width:225px;">
<option value=0 selected></option>
<?php
include 'connect.php';
$q  = "SELECT attribute_id,attribute_name FROM attribute ";
$result = $db->query($q);
while ($row1 = $result->fetch()){
  echo "<option value='$row1[attribute_name]'>$row1[attribute_name]</option>";
}
?>
</select>

1 个答案:

答案 0 :(得分:0)

首先,您的选择级别每个都需要不同的名称 还有一个表单,还有一个提交按钮...... 就这样....

<form method="post" action="process.php">
 <select name="level1[]">
  <!--Your database options lists-->
 </select>
 <select name="level1[]">
  <!--Your database options lists-->
 </select>
 <select name="level1[]">
  <!--Your database options lists-->
 </select>     
 <input type="submit" value="SUBMIT"/>
</form>

然后当你选择所有三个并点击提交时,它会转到process.php 这个文件是哪个......

<?php

  $levels = implode(",", $_POST['level1']);

  //Now you have a string called $levels
  // Which contains a comma seperated list, to insert into db / one field

  //Insert into your table...change your table and field names to real values...

  $sql = "INSERT INTO yourTable (attributes) VALUES (:attributes)";
  $q = $db->prepare($sql);
  $q->execute(array(':attributes'=>$levels));
  ?>

这将处理表格......

注意$ levels变量...这是传递给数据库的字符串,由你的选择框构建。

你可以用两种方式构建它......

字符串连接。 像这样......

$levels = $_POST['level1'][0].",".$_POST['level1'][1].",".$_POST['level1'][2];

或者构建一个数组并内爆,进入逗号分隔列表.. 像这样......

$levels = implode(",", $_POST['level1']);

无论哪种方式......你最终会得到一个逗号分隔的字符串进入你的数据库......

YOURE WELCOME:)