使用1(插入)按钮,我可以将HTML表中的多行数据插入MySQL数据库表中吗


Using 1 (insert) button, can I insert multiple row data from an HTML table to a MySQL database table?

我随机生成了一些问题,并决定将它们放在一个临时表中。现在,它已经在我的html表中了:

echo "<form action=# method=post>";
echo "<table>";
do{
echo "<tr>";
echo "<td>"."<input type=text value='$rows[question]' name=question>"."</td>;
echo "<td>"."<input type=text value='$rows[correct]' name=correct>"."</td>;
}while($rows=mysqli_fetch_array($rs));
echo "</table>";

echo "<input type=submit name=insert>";
echo "</form>";
?>
</body>
</html>

我想做的是,当我点击插入按钮name="insert"时,该表中的数据[从第1行到最后一行]将被插入到我的数据库"tbl_randomq"中。只需单击一下,是否可以同时将多行数据插入数据库。

我尝试过使用while循环,但它只插入来自最后一行的重复(10次)数据。请帮助:-)

do{
?>
<tr>
    <td><input type=text value='<?=$row["question"]?>' name='question[]'></td>
    <td><input type=text value='<?=$row["correct"]?>' name='correct[]'></td>    
</tr>
<?php
}while($row = mysqli_fetch_array($rs));

然后保存:

for ($i=0; $i<count($_POST['question']); $i++){
    $question = addslashes($_POST['question'][$i]);
    $correct = addslashes($_POST['correct'][$i]);
    mysqli_query("
                      insert into tbl_ramdomq (question, correct) 
                      values ('$question','$correct')
    "); 
}

根据需要将代码格式替换为自己的格式。尽管对我自己来说,我更喜欢在html中使用短的php标记,而不是echo,因为语法着色和更好的清晰度。