Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.8k views
in Technique[技术] by (71.8m points)

php - Inserting multiple entries into table from the same form

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...