-1

任何人都可以帮我解决这个代码,我有几个下拉列表,我想插入到表格中。但我只需要将它插入一个字段,任何人都有一个绝妙的主意:)

这是下拉列表的示例。

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

1 回答 1

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']);

无论哪种方式...您最终都会以逗号分隔的字符串输入您的数据库...

别客气 :)

于 2013-06-11T03:35:45.263 回答