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

时间:2021-08-09 06:51:40

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". Is it possible to insert multiple row data simultaneously to database with just 1 click.

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

I've tried to use while loop, but it only inserts repeated(10times) data coming from the last row. Help with this please :-)

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

1 个解决方案

#1


1  

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标签,而不是回声,因为语法着色和更好的清晰度。

#1


1  

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标签,而不是回声,因为语法着色和更好的清晰度。