几个下拉列表输入到一个字段表 [英] Several drop down list input to one field table

查看:87
本文介绍了几个下拉列表输入到一个字段表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这是下拉列表的示例。

 < select name =level1style =width:225px;> 
< option value = 0 selected>< / option>
<?php
包含'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 =level1style =width:225px;>
< option value = 0 selected>< / option>
<?php
包含'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 =level1style =width:225px;>
< option value = 0 selected>< / option>
<?php
包含'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>


解决方案

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

 < form method =postaction =process.php> 
< select name =level1 []>
<! - 您的数据库选项列表 - >
< / select>
< select name =level1 []>
<! - 您的数据库选项列表 - >
< / select>
< select name =level1 []>
<! - 您的数据库选项列表 - >
< / select>
< input type =submitvalue =SUBMIT/>
< / form>

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

 <?php 

$ levels = implode(,, $ _POST [ '级别1']);

//现在你有一个名为$ levels的字符串
//其中包含逗号分隔的列表,可以插入到db / one字段

//插入你的表...将你的表和字段名称更改为实际值...

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

这将处理表单...



注意$ levels变量......这就是从你的选择框构建的传入数据库的字符串。



你可以通过两种方式构建它...



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

  $ levels = $ _POST ['level1'] [0]。, 。$ _ POST [ '(第一级)'] [1]  $ _ POST [ '(第一级)'] [2]。。 

或者建立一个数组和implode,放入逗号分隔列表中。
像这样.. 。

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

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



年轻的欢迎:)


Guys anyone can help me solved this code, i have several drop down list which i want to insert into table. but i need to insert it into one field only, anyone have a brilliant idea how to do it:)

this is example for drop down list.

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

解决方案

First your select levels each need a different name and a form to go into, with a submit button as well... So like this....

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

Then when you pick all three and hit submit it goes to process.php Which is this file...

<?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));
  ?>

This will process the form...

Notice the $levels variable...thats the string to pass into the db, built from your select boxes.

You can build it two ways...

Either string concatenation. Like this...

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

Or build an array and implode, into comma seperated list.. Like this...

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

Either way...youll end up with a comma seperated string to enter into your database...

YOURE WELCOME :)

这篇关于几个下拉列表输入到一个字段表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆