php 将php表单中的多行插入到数据库中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19077724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inserting multiple rows from a php form into the database
提问by jhe
my form as the following table (of textfeild). I am feeding the values into these textfeild. How can i enter these values into my database? plz help.
我的表格如下表(textfeild)。我正在将这些值输入到这些 textfeild 中。如何将这些值输入到我的数据库中?请帮忙。
code of the form:-
表格代码:-
$rows=$_POST["rows"];
echo "
<table align=\"center\" border=\"1\" width=\"70%\">
<tr>
<td><b>Roll No.</b></td> <td><b>Full Name</b></td> <td><b>Unit_test</b> </td><td><b>Prelims</b></td>
<td><b>Attendance</b></td> <td><b>File</b></td>
</tr>";
for($n=0;$n<$rows;$n++)
{
echo "<tr>
<td><input type=\"text\" name=\"roll\" /></td><td><input type=\"text\" name=\"name\" /></td><td><input type=\"text\" name=\"unit_test\" /></td>
<td><input type=\"text\" name=\"prelims\" /></td><td><input type=\"text\" name=\"attendance\" /></td><td><input type=\"text\" name=\"file\" /></td>
</tr>";
}
echo "</table>";
echo "<form action=\"feed2.php\" method=\"post\">
<br>
<input type=\"submit\" name=\"\" value=\"Submit\" />
</form>";
code used for inserting into db:-
用于插入数据库的代码:-
for($n=0;$n<3;$n++)
{
if(isset($_POST['roll']))
{
echo "<br>Updating";
$roll=$_POST['roll'];
$name=$_POST['name'];
$unit_test=$_POST['unit_test'];
$prelims=$_POST['prelims'];
$attendance=$_POST['attendance'];
$file=$_POST['file'];
$sql="Insert into students (roll,name,unit_test,prelims,average,attendance,file,interm)
VALUES ('$roll','$name','$unit_test','$prelims','$average'=(('$unit_test' + '$prelims')/125) *10,'$attendance','$file','$average'+'$attendance'+'$file')";
$res=mysql_query($sql);
header("Location:./view.php");
}
回答by Clart Tent
The simplest way is probably to make the $_POST values for roll etc. an array (so $_POST becomes a multidimensional array).
最简单的方法可能是将 roll 等的 $_POST 值设为一个数组(因此 $_POST 成为一个多维数组)。
See this previous question for an example of how it can be done:
有关如何完成的示例,请参阅上一个问题:
Submitting a multidimensional array via POST with php
You can then loop through the array values in your database insert code.
然后,您可以遍历数据库插入代码中的数组值。
回答by asim-ishaq
The name of each field must be different in each row in order to successfully send data from browser to server. In your case for all columns the name remains the same in all rows. You can suffix them with $n
每一行中每个字段的名称必须不同,才能成功地将数据从浏览器发送到服务器。在您的情况下,所有列的名称在所有行中都保持不变。你可以用 $n 后缀
Solution:
解决方案:
Replace the code which generate fields to the following:
将生成字段的代码替换为以下内容:
for($n=0;$n<$rows;$n++)
{
echo "<tr>
<td><input type=\"text\" name=\"roll_$n\" /></td><td><input type=\"text\" name=\"name_$n\" /></td><td><input type=\"text\" name=\"unit_test_$n\" /></td>
<td><input type=\"text\" name=\"prelims_$n\" /></td><td><input type=\"text\" name=\"attendance_$n\" /></td><td><input type=\"text\" name=\"file_$n\" /></td>
</tr>";
}
In above code we have suffixed each field with _$n
在上面的代码中,我们为每个字段添加了后缀 _$n
Secondly, Change the code of insert to the following:
其次,将insert的代码改成如下:
$roll=$_POST['roll_'.$n];
$name=$_POST['name_'.$n];
$unit_test=$_POST['unit_test_'.$n];
$prelims=$_POST['prelims_'.$n];
$attendance=$_POST['attendance_'.$n];
$file=$_POST['file_'.$n];
Thirdly, I think the sql query need to be changed. Calculate average before the query and store its values in a variable. Then reference this variable in query same as you have done with other variables like $roll and $name.
第三,我认为sql查询需要更改。在查询之前计算平均值并将其值存储在变量中。然后在查询中引用这个变量,就像你对 $roll 和 $name 等其他变量所做的一样。
$average=(($unit_test + $prelims)/125) *10;
Now with this code all three lines will be saved in database.
现在使用此代码,所有三行都将保存在数据库中。
Hope this helps.
希望这可以帮助。
回答by John
Multiple row inserts in SQL are simply multiple parenthesis with commas, (), (), ();
SQL 中的多行插入只是带逗号的多个括号, (), (), ();
You are also NOTescaping your data! You must escape ALLyour data regardless of text or binary. It is simple and you must never fail to escape data. I escape data even if it's come directly out of my database, a hacker could post code in say a blog comment, it gets commented out, but then for some reason if I handle that comment it could later execute.
你也没有逃避你的数据!无论是文本还是二进制,您都必须转义所有数据。这很简单,您必须永远无法逃避数据。我会转义数据,即使它直接来自我的数据库,黑客可以在博客评论中发布代码,它会被注释掉,但是由于某种原因,如果我处理该评论,它可以稍后执行。
$roll = mysql_real_escape_string($_POST['roll');
So just do this...
所以就这样做...
INSERT INTO table (roll, name, unit_test, prelims, average, attendance, file, interm) VALUES
('$roll','$name','$unit_test','$prelims','$average'=(('$unit_test' + '$prelims')/125) *10,'$attendance','$file','$average'+'$attendance'+'$file'),
('$roll','$name','$unit_test','$prelims','$average'=(('$unit_test' + '$prelims')/125) *10,'$attendance','$file','$average'+'$attendance'+'$file'),
('$roll','$name','$unit_test','$prelims','$average'=(('$unit_test' + '$prelims')/125) *10,'$attendance','$file','$average'+'$attendance'+'$file'),
('$roll','$name','$unit_test','$prelims','$average'=(('$unit_test' + '$prelims')/125) *10,'$attendance','$file','$average'+'$attendance'+'$file');