【问题标题】:Inserting multiple entries into table from the same form将多个条目从同一个表单插入到表中
【发布时间】:2013-04-03 05:42:56
【问题描述】:

这是我的表格,很简单。我有 3 个文本字段,将在其中输入问题,我想将其中的每一个都放入数据库中。

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

当我运行文件时,它给了我以下错误:

Insert failed: Unknown column 'field1' in 'field list' in quest.php on line 15

谁能告诉我查询中是否有错误以及如何解决?任何帮助表示赞赏:)

【问题讨论】:

  • 错误信息是不言自明的。您的 db 表中没有名为 field1 的列。
  • 是不是很简单?它只是说您名为paper 的表没有名为field1 的字段; INSERT INTO paper (field1)

标签: php mysql database insert


【解决方案1】:

发布的值在此索引中:$_POST['field1'][$i]['f​​ield1']。因此,您在 for 循环中使用以下代码: $insertArr[] = "('" .$_POST['field1'][$i]['f​​ield1']. "')" ;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-18
    • 1970-01-01
    • 2021-04-06
    • 1970-01-01
    相关资源
    最近更新 更多