简体   繁体   English

如何使用php将html中的表格导出到数据库?

[英]How to export table in html to database using php?

I am trying to export a full table into SQL using data from an HTML table.我正在尝试使用 HTML 表中的数据将完整表导出到 SQL 中。 I know how to export one row, but can't understand how to export multiple rows.我知道如何导出一行,但无法理解如何导出多行。 Any advice?有什么建议吗?

<?php while ($row = $result->fetch_assoc()) :?>
<tr>
<form action="insertorder.php" method="post">
    <td name="Item_ID[]"><?=$row["item_id"]?></td>
    <td name="name[]"><?=$row["ITEM_NAME"]?></td>
    <td name="suggested_qty"><?=$row["suggested_qty"]?></td>
    <td  name="price" class="pricetd"><?=$row["Price_item"]?></td>
    <td>
    <input type="text" name="editedvalues[]" class="qtyinput" value="<?=$row["suggested_qty"]?>" />
    </td>
    <td><input name='result[]' class="resultinput" /></td>
</tr>

<?php endwhile?>
<input type="submit" class="btn btn-dark" value="Submit">
</table>
</form>

Export script:导出脚本:

$sql = "INSERT INTO ms_order VALUES (item_id, item_name, order_quantity, total)";
for ($i=0; $i<count($_POST['Item_ID']); $i++) {
    $sql .= '(\'' . $_POST['Item_ID'][$i] . '\', \'' . $_POST['name'][$i] . '\', \'' . $_POST['editedvalues'][$i] . '\', \'' . $_POST['result'][$i] . '\')';
    if ($i<count($_POST['Item_ID']) - 1) {
       $sql .= ',';
    }
    echo $sql;
}

The problem on your code it's that you're creating a new form for each fetched row on the while loop.代码的问题在于您正在为 while 循环中的每个获取的行创建一个新表单。 By doing this, you're sending only one row whenever the form is submitted.通过这样做,您在提交表单时只发送一行。 To solve this you should place the form tag outside the while loop.要解决这个问题,您应该将 form 标记放在 while 循环之外。 Also, let me share with you a cleaner way to accomplish what you're doing:另外,让我与您分享一种更简洁的方法来完成您正在做的事情:

$sql = "INSERT INTO ms_order (item_id, item_name, order_quantity, total) VALUES ";
    $sql .= implode(',', array_map(function($item_id, $name, $editedvalues, $result) {
                return <<<SQL
            ({$item_id}, {$name}, {$editedvalues}, {$result})
            SQL;
            }, $_POST['Item_ID'], $_POST['name'], $_POST['editedvalues'], $_POST['result']));

This is what it does:这是它的作用:

array_map function fetchs each index of a given array, you can pass as an argument as many arrays as you wish, it will iterate over each index so you will be able to use each value to manipulate it and return it. array_map函数获取给定数组的每个索引,您可以将任意数量的数组作为参数传递,它将遍历每个索引,因此您将能够使用每个值来操作它并返回它。

Each iteration of the array_map will create a new array, so in the end what you get is an array of arrays containing the returned results. array_map每次迭代都会创建一个新数组,所以最终你得到的是一个包含返回结果的数组数组。 Finally, as you want to make a multiple INSERT , you concatenate each array with a colon using the implode function.最后,当您想要创建多个INSERT ,您可以使用implode函数将每个数组与一个冒号连接起来。

Note that you should sanitize the $_POST values as you might be vulnerable to SQL Injection and other security issues.请注意,您应该清理 $_POST 值,因为您可能容易受到 SQL 注入和其他安全问题的影响。 For further information about it, check it out here .有关它的更多信息,请在此处查看

Greetings,你好,

Matt马特

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

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