简体   繁体   English

将列表框选项值保存到mysql中,然后再次显示php mysql

[英]save list box option value into mysql then again display php mysql

I want to save list option values into MySQL then again want to display them as a list. 我想将列表选项值保存到MySQL中,然后再次希望将它们显示为列表。

I need that for a dynamic query. 我需要一个动态查询。

 <select multiple id="to" size="15" name="to[]">
    <option value="a">a</option>
    <option value="b">b</option>
    <option value="c">c</option>
    <option value="d">d</option>
 </select>

This is php code to change it into (a,b,c,d): 这是将其更改为(a,b,c,d)的php代码:

   $joinedString = array();
   $var1 = $_POST['to'];
   $joinedString = implode(',', $var1);
   $joinedString;

I just change this into a,b,c,d with implode function, and its working fine. 我只是将其更改为具有爆破功能的a,b,c,d,并且可以正常工作。

But don't know how to save it in mysql and how to again fetch data from database and make this as: 但是不知道如何将其保存在mysql中以及如何再次从数据库中获取数据并将其设置为:

 <select multiple id="to" size="15" name="to[]">
    <option value="a">a</option>
    <option value="b">b</option>
    <option value="c">c</option>
    <option value="d">d</option>
</select>

Now that you have the string a,b,c,d in variable $joinedString , Just use it to insert into your database. 现在,您在变量$joinedString具有字符串a,b,c,d,只需将其插入数据库即可。 Remember you need to insert this string like this 'a,b,c,d' and not simply a,b,c,d. 请记住,您需要像“ a,b,c,d”这样插入字符串,而不仅仅是a,b,c,d。 So, first format your string in inverted commas like 'a,b,c,d'. 因此,首先将字符串格式设置为“ a,b,c,d”之类的反逗号。 Now, that you have done it, just use insert command. 现在,您已经完成了操作,只需使用insert命令即可。

Insert into Table_name set column_name='".$joinedString."' where some_condition. 

After that, you retrieve it if you have to use it again, using "Select" query. 之后,使用“选择”查询检索是否需要再次使用它。

Select column_name from Table_name where Some_condition

It would give you your variable back to you in format, 'a,b,c,d'. 它会以'a,b,c,d'的格式将您的变量返回给您。 Save it in some variable $variable. 将其保存在变量$ variable中。 Now you use explode function to make an array of this string using comma. 现在,您可以使用explode函数使用逗号来创建此字符串的数组。

$arr_string  = explode(",",$variable);
This $arr_string would be an array like this 

array(0=> a,
      1=> b,
      2=> c,
      3=> d)

Now you loop over this array using foreach and make the select option. 现在,您可以使用foreach遍历此数组,并进行选择。

<select multiple id="to" size="15" name="to[]">
<?php 
     foreach($variable as $key=>$val)
     {
       echo "<option value='".$val."'>".$val."</option>";
     }
?>
</select>

If you are storing comma separated value within database you can get the value from database using query and from result you can simply implement that within your file using explode function of PHP as : 如果要在数据库中存储逗号分隔的值,则可以使用查询从数据库中获取值,并可以从结果中使用PHP的explode函数在文件中简单地实现该值:

<?php
$joinedString = 'a,b,c,d'; // fetched data from database using query if you are storing data within database comma separated
$data = explode(',',$joinedString);
?>
<select multiple id="to" size="15" name="to[]">
    <?php foreach($data as $key => $value){?>
    <option value="<?php echo $value; ?>"><?php echo $value; ?></option>
    <?php } ?>
</select>

I think this might be the expected answer.. 我认为这可能是预期的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM