从同一表单向表中插入多个条目 [英] Inserting multiple entries into table from the same form
问题描述
这是我的表格,非常简单。我有3个文本字段,可在其中输入问题,我想将每个问题都放入数据库中。
This is my form, a pretty simple one. I have 3 text fields in which questions will be entered and i want to put each of these in a database.
questFormtest.php:
<html>
<head><title> Test Quest</title></head>
<body>
<form id= "qform" method="post" action="quest.php">
<h3>Enter Questions</h3><br><br>
<h3>Question 1: Five marks each.<br></h3>
a) <input type="text" name="field1[][field1]" size=45>* <br><br>
b) <input type="text" name="field1[][field1]" size=45>* <br><br>
c) <input type="text" name="field1[][field1]" size=45>* <br><br>
<p><input type="submit" name="submit" value="Submit" align="center" />
<input type='reset' name='Cancel' value='Cancel' /></p>
</form>
</body>
</html>
My php file is as follows:
quest.php:
<?php
include('connectionfile.php');
$cnt = count($_POST['field1']);
if ($cnt > 0) {
$insertArr = array();
for ($i=0; $i<$cnt; $i++) {
$insertArr[] = "('" .$_POST['field1'][$i]. "')";
}
$query = "INSERT INTO paper (field1) VALUES " . implode(", ", $insertArr);
mysql_query($query) or trigger_error("Insert failed: " . mysql_error());
}
mysql_close($id_link);
?>
运行文件时,出现以下错误:
When i run the file, it gives me the following error:
Insert failed: Unknown column 'field1' in 'field list' in quest.php on line 15
有人可以告诉我查询中是否有错误,我该如何解决?感谢您的帮助:)
Can somebody tell me if there's an error in the query and how i can solve it? Any help is appreciated :)
推荐答案
发布的值在此索引中:$ _POST ['field1'] [$ i] ['field1']。因此,您可以使用以下代码:$ insertArr [] =('。$ _ POST ['field1'] [$ i] ['field1']。’) for循环;
The posted value is in this index: $_POST['field1'][$i]['field1']. So you use this code : $insertArr[] = "('" .$_POST['field1'][$i]['field1']. "')" in you for loop;
这篇关于从同一表单向表中插入多个条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!