php 从多选列表(html表单)向mysql数据库插入数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21562441/
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-08-25 04:15:20  来源:igfitidea点击:

insert data to mysql database from multiple select list (html form)

phpmysql

提问by user3272462

I make a form, where there is ID of a shop:

我制作了一个表格,其中有商店的 ID:

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

在我选择选项后,我必须将它们传递给数据库中的一个表;表由 2 列组成:shopId 和 car。问题是它只传递一个选项,不可能像在一家商店的两个或三个模型中那样将几行添加到表格中。我想我必须像数组或其他东西一样传递数据。你能帮我吗。

$shopId = $_GET["shopId"];
$cars = $_GET["cars"];

this is a query:

这是一个查询:

$query = "INSERT INTO shops (shopId, car) VALUES ($shopId, $cars)";

回答by Armin

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. I'd do it like this:

我会说考虑到限制,你唯一的选择是将所有选定的选项组合成一个逗号分隔的字符串(使用 PHP 的内置函数 implode http://php.net/implode),然后插入 shopID并将以逗号分隔的汽车列表放入新行。我会这样做:

<?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[]. 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. 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>元素的名称,name="cars[]"并密切注意单词后面的开/关方括号cars[]。这将允许通过表单传递多个选项,而不是只有一个。正如@bart2puck 在他的解决方案中提到的,这是一个关键的区别,不应被忽视。此外,允许用户选择多个选项的对浏览器最友好的方式是multiple="multiple"在您的<select>元素中使用该属性。

回答by bart2puck

you are getting only 1 insert because you are setting the value of $_GET['cars'] to the last selected item in your multiple. 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.

您只得到 1 个插入,因为您将 $_GET['cars'] 的值设置为多个中的最后一个选定项目。要实现您要查找的内容,请将汽车的选择名称设置为汽车 []。当您转到处理此内容时,您现在将在 $_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:

如果您只能有 1 个插入,这看起来很奇怪,那么请执行以下操作:

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

你最终会得到一个像“本田、马自达、丰田”这样的领域,这似乎效率不高。