[英]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循環,但是它僅插入來自最后一行的重復(10times)數據。 請幫助這個:-)
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
因為語法着色和更好的清晰度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.