[英]insert data to mysql database from multiple select list (html form)
I make a form, where there is ID of a shop: 我做了一个表格,里面有商店的身份证明:
<input type="text" name="shopId">
and there is a multiple choice select: 并且有多项选择:
<select name="cars" multiple required>
after i get selected options, i have to pass them to a table in the database; 在我获得选定的选项后,我必须将它们传递给数据库中的表; table consists of 2 columns: shopId and car. 表由2列组成:shopId和car。 The thing is it passes only one option and it is impossible to have a few rows added to the table like in one shop two or three models. 问题是它只传递了一个选项,并且不可能像在一个商店中的两个或三个模型那样将几行添加到表中。 I suppose i have to pass the data like an array or something. 我想我必须像数组一样传递数据。 Can you help me, please. 你能帮我吗。
$shopId = $_GET["shopId"];
$cars = $_GET["cars"];
this is a query: 这是一个查询:
$query = "INSERT INTO shops (shopId, car) VALUES ($shopId, $cars)";
I'd say given the constraints, the only option you have is to combine all the selected options into a single comma separated string (using PHP's built-in function called implode http://php.net/implode ), then insert the shopID and the comma-separated-list of cars into a new row. 我会说在给定约束的情况下,唯一的选择就是将所有选中的选项组合成一个逗号分隔的字符串(使用PHP的内置函数implode http://php.net/implode ),然后插入shopID和逗号分隔的汽车列表成新行。 I'd do it like this: 我这样做:
<?php
if ($_POST) {
$cars_string = implode(', ', $_POST['cars']);
$sql = '
INSERT INTO
`my_table` (
`shopID`,
`cars`
)
VALUES (
'. $_POST['shopID'] .',
"'. $cars_string .'"
)
';
mysql_query($sql) OR die(mysql_error());
}
?>
<form method="post" action="">
Shop ID: <input type="text" name="shopID"/> -
<select name="cars[]" multiple="multiple">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="honda">Honda</option>
<option value="audi">Audi</option>
<option value="bmw">BMW</option>
</select>
<input type="submit" name="Submit"/>
</form>
This is the best solution given the constraints you've provided . 考虑到您提供的限制,这是最佳解决方案。 However, I do not see the logic in only being able to add a single row per form submit. 但是,我没有看到逻辑只能为每个表单提交添加一行。 That is not a good database design, especially in the long-term. 这不是一个好的数据库设计,特别是从长远来看。
Please notice how the <select>
element has the name of name="cars[]"
and pay close attention to the open/close square brackets after the word cars[]
. 请注意<select>
元素的名称是name="cars[]"
并密切关注单词cars[]
后面的打开/关闭方括号。 This will allow multiple options to be passed through the form, instead of only one. 这将允许多个选项通过表单传递,而不是只传递一个。 This is a critical difference and it should not be overlooked, as @bart2puck mentions in his solution. 这是一个至关重要的区别,不应该被忽视,正如@ bart2puck在他的解决方案中提到的那样。 Also, the most browser-friendly way to allow users to select multiple options is to use the attribute multiple="multiple"
in your <select>
element. 此外,允许用户选择多个选项的最易浏览的方法是在<select>
元素中使用属性multiple="multiple"
。
you are getting only 1 insert because you are setting the value of $_GET['cars'] to the last selected item in your multiple. 您只获得1次插入,因为您将$ _GET ['cars']的值设置为多个中的最后一个选定项目。 to acheive what you are looking for set the select name of cars to cars[]. 实现你想要的东西设置汽车的选择名称[]。 When you goto process this you will now have an array in $_GET data. 当你转到处理它时,你现在将在$ _GET数据中有一个数组。
then loop through that to do your inserts. 然后循环执行插入操作。
$shopId = $_GET['shopId'];
foreach ($_GET['cars'] as $value)
{
$ins = "INSERT INSERT INTO shops (shopId, car) VALUES ($shopId, $value)";
}
if you can only have 1 insert, which seems odd, then do something like: 如果你只能有一个插入,这似乎很奇怪,那么做一些像:
$cars = "";
foreach ($_GET['cars'] as $value)
{
$cars .= $value . ",";
}
$cars = substr($cars,0,-1); //to remove the last comma
then 然后
$ins = "INSERT INSERT INTO shops (shopId, car) VALUES ($shopId, $cars)";
you are going to end up with a field like 'honda,mazda,toyota' and this doesn't seem very efficient. 你将最终得到像'本田,马自达,丰田'这样的领域,这似乎并不是很有效率。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.