简体   繁体   English

使用1(插入)按钮,我可以从HTML表向MySQL数据库表插入多行数据吗?

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

I have randomly generated questions and decided to put those in a temporary table. 我随机产生了问题,并决定将其放在临时表中。 Now, it is already in my html 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>

What I want to happen is, when I hit the insert button name="insert", data from that table, [from row 1 till the last row] will be inserted to my database "tbl_randomq". 我想要发生的是,当我按下插入按钮name =“ insert”时,该表中的数据(从第1行到最后一行)将插入到我的数据库“ tbl_randomq”中。 Is it possible to insert multiple row data simultaneously to database with just 1 click. 只需单击一下就可以同时向数据库中插入多个行数据。

I've tried to use while loop, but it only inserts repeated(10times) data coming from the last row. 我尝试使用while循环,但是它仅插入来自最后一行的重复(10times)数据。 Help with this please :-) 请帮助这个:-)

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));

Then to save this : 然后保存:

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')
    "); 
}

Replace code formatting with your own as you wish. 您可以根据自己的意愿替换代码格式。 Though for myself I prefer to use short php tags inside html rather than echo because of syntax coloring and better clarity. 尽管对我自己来说,我更喜欢在html内使用短php标签,而不是echo因为语法着色和更好的清晰度。

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

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