javascript 从选择多个中获取值并将它们放入数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6148818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 19:39:50  来源:igfitidea点击:

Get values from select multiple and get them in to database

phpjavascriptjqueryhtmlcss

提问by gambozygame

What is the best way to get the all values of one multiple select box, and then i want to save those values in one row in the database, then from that row i want to get each value separate. What is the best way to do such a thing ? My goal is to save the values and then get each separate.

获取一个多选框的所有值的最佳方法是什么,然后我想将这些值保存在数据库中的一行中,然后从该行中我想将每个值分开。做这样的事情的最好方法是什么?我的目标是保存这些值,然后将每个值分开。

回答by Dormouse

You could do it this way

你可以这样做

<select name="foo[]" multiple="multiple">
     <option value="dog">Dog</option>
     <option value="cat">Cat</option>
     <option value="fish">Fish</option>
</select>

<?php

$pets = $_POST['foo'];

//sanitize and verify the input here. Escape properly and what not

$query = mysql_query("INSERT INTO `pets` (`pet1`, `pet2`, `pet3`) VALUES ('".$pets[0]."', '".$pets[1]."',  '".$pets[2]."')");
?>

But to be honest it's an awful way to go about building a database. It'll be very annoying or difficult to do anything meaningful with.

但老实说,这是一种构建数据库的糟糕方式。做任何有意义的事情会非常烦人或困难。

Why not have the following database setup:

为什么不进行以下数据库设置:

users:

用户:

id | name 
----------
 1 | Tim

pets:

宠物:

id | user_id | type
-------------------
1  | 1       | Fish
2  | 1       | Cat
3  | 4       | Kakapo

And then you would have a much more easily searchable and manipulatable database that's consistent.

然后,您将拥有一个更易于搜索和操作的一致数据库。

回答by Gabriel Spiteri

I believe you should receive the value selected as an array in the $_POST variable so say your select has a name="products"

我相信您应该收到在 $_POST 变量中选择为数组的值,所以说您的选择有一个 name="products"

The values selected will be in $_POST["products"] assuming you are submitting the form via POST.

假设您通过 POST 提交表单,所选值将在 $_POST["products"] 中。

Then you can use the implode function to generate a string. For example:

然后就可以使用内爆函数生成字符串了。例如:

$myProducts = implode("|", $_POST["products"]); //This will give you a pipeline delimeted string like : computer|laptop|monitor

$myProducts = mysql_real_escape_string($myProducts); //Just to santize before inserting in DB

Then just insert that string into the DB.

然后将该字符串插入到数据库中。

When retrieving the data you can reverse the process by using the explode function:

检索数据时,您可以使用爆炸函数反转该过程:

$myProducts = explode("|" , [The value retrieved from the database]); //This will give you an array which you can iterate and thus accessing the values individually.

Hope this helps :)

希望这可以帮助 :)

回答by royrui

if i understand your question well...

如果我理解你的问题...

$dataFromMultipleBox = array('data1', 'data2', 'data3');
$data = implode("||", $dataFromMultipleBox);

/*
After that,
write $data into database

fetch from the database again
*/

$pieces = explode("||", $rowFromDatabase);

foreach($pieces as $value) {
  echo $value;
  echo '<br>';
}

回答by Quentin

What is the best way to get the all values of one multiple select box,

获取一个多选框的所有值的最佳方法是什么,

Since it is PHP. Give the select element a name ending in [], then you can access them as $_POST['foo'][]

既然是PHP。为 select 元素指定一个以 结尾的名称[],然后您可以将它们作为$_POST['foo'][]

and then i want to save those values in one row in the database, then from that row i want to get each value separate.

然后我想将这些值保存在数据库中的一行中,然后从该行中我想将每个值分开。

Don't do that. Have a second table with two columns. The id (as a foreign key) of the row in the other table (where you were planning to store the data) and the value itself.

不要那样做。有一个有两列的第二个表。另一个表(您计划存储数据的位置)中行的 id(作为外键)和值本身。