[英]how to send multiple values with multiple select input
I need to know which will be better to insert multiple values into table with multiple select input with also more than one value.我需要知道哪个更好地将多个值插入到具有多个选择输入的表中,并且具有多个值。
Is better with one value sepparated with "|"最好用“|”分隔一个值each value or is better with data-id, date-price, etc?每个值还是数据 ID、日期价格等更好?
Here the input with the first form:这里是第一种形式的输入:
<select class="form-control form-control-lg" name="toBuy[]" id="multi" multiple="multiple" style="width:100%">
<?php
$sql = $conn->prepare("SELECT id, name, price FROM ITEMS WHERE active != 0");
$sql->execute();
while($row = $sql->fetch(PDO::FETCH_ASSOC)) {
echo '<option value="'.$row['name'].'|'.$row['id'].'|'.$row['price'].'">'.$row['name'].'</option>';
}
?>
</select>
Here the input with the second form:这里是第二种形式的输入:
<select class="form-control form-control-lg" name="toBuy[]" id="multi" multiple="multiple" style="width:100%">
<?php
$sql = $conn->prepare("SELECT id, name, price FROM ITEMS WHERE active != 0");
$sql->execute();
while($row = $sql->fetch(PDO::FETCH_ASSOC)) {
echo '<option value="'.$row['name'].'" data-id="'.$row['id'].'" data-price="'.$row['price'].'">'.$row['name'].'</option>';
}
?>
</select>
Now in the insert file, how can I add those value into the table?现在在插入文件中,如何将这些值添加到表中?
$sql2 = "INSERT INTO PAID (idItem, name, price, idUser)
VALUES ";
$insertQuery2 = array();
$insertData2 = array();
foreach ($_POST['id'] as $i => $item) {
//WITH THE FIRST FORM
$result = explode('|', $_POST['toBuy']);
$result.[0];
$result.[1];
$result.[2];
$insertQuery2[] = '(?, ?, ?, ?)';
$insertData2[] = $result.[0][$i];
$insertData2[] = $result.[1][$i];
$insertData2[] = $result.[2][$i];
$insertData2[] = $idUser;
//SECOND FORM
$insertQuery2[] = '(?, ?, ?, ?)';
$insertData2[] = $_POST['id'][$i];
$insertData2[] = $_POST['name'][$i];
$insertData2[] = $_POST['price'][$i];
$insertData2[] = $idUser;
}
if (!empty($insertQuery2)) {
$sql2 .= implode(', ', $insertQuery2);
$stmt2 = $conn->prepare($sql2);
$stmt2->execute($insertData2);
}
You only need to use the item_id and user_id for this one.您只需要为此使用 item_id 和 user_id。 Data related to the item is already stored in your item table.与该项目相关的数据已存储在您的项目表中。 What you need to do here is push all the selected item_id's into an array and use a loop when inserting values.您需要在这里做的是将所有选定的 item_id 推送到一个数组中,并在插入值时使用循环。
First, as mentioned before, you only need:首先,如前所述,您只需要:
echo '<option value="'.$row['id'].'">'.$row['name'].'</option>';
With the multiple option you should get an array of chosen ids.使用 multiple 选项,您应该获得一组选定的 id。 Then your SQL string could be (with added sanitation):然后你的 SQL 字符串可以是(增加了卫生条件):
$ids = filter_input(
INPUT_POST,
'toBuy',
FILTER_SANITIZE_NUMBER_INT,
FILTER_REQUIRE_ARRAY
);
$data = array_merge([$idUser], $ids);
$inparams = implode(',', array_fill(0, count($ids), '?'));
$sql = "INSERT INTO PAID (idItem, name, price, idUser) ";
$sql.= "SELECT id, name, price, ? ";
$sql.= "FROM ITEMS ";
$sql.= "WHERE id IN ($inparams)";
$stmt = $conn->prepare($sql);
$stmt->execute($data);
Voila, this should do it.瞧,这应该这样做。 Maybe there's an error in it, I coded it on the fly without testing, but maybe you get the idea.也许它有一个错误,我在没有测试的情况下即时编码,但也许你明白了。
For HTML SELECT OPTION examination and sanitation see:对于 HTML SELECT OPTION 检查和卫生,请参阅:
https://www.phptutorial.net/php-tutorial/php-select-option/ https://www.phptutorial.net/php-tutorial/php-select-option/
For SQL INSERT INTO SELECT tutorial see:有关 SQL INSERT INTO SELECT 教程,请参阅:
https://www.w3schools.com/SQL/sql_insert_into_select.asp https://www.w3schools.com/SQL/sql_insert_into_select.asp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.