从php表单插入多行到数据库 [英] Inserting multiple rows from a php form into the database
本文介绍了从php表单插入多行到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的表格如下表(textfeild).我正在将这些价值观充实到这些文本中.如何将这些值输入数据库?请帮助.
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.
格式的代码:-
$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>";
用于插入db的代码:-
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");
}
推荐答案
最简单的方法可能是使roll等的$ _POST值成为一个数组(因此$ _POST成为多维数组).
The simplest way is probably to make the $_POST values for roll etc. an array (so $_POST becomes a multidimensional array).
请参见前面的问题,以获取有关如何完成此操作的示例:
See this previous question for an example of how it can be done:
然后您可以遍历数据库插入代码中的数组值.
You can then loop through the array values in your database insert code.
这篇关于从php表单插入多行到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文