且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将动态表行数据立即插入数据库

更新时间:2023-12-04 12:53:22

这里有些事情可以使您的生活变得更轻松.

There are a few things here that will make your life easier if changed.

具有相同名称的输入元素将按照它们出现的顺序作为数组提交.标准过程是这样布局html(为简洁起见,删除了一些字段);

Input elements with the same name will be submitted as an array, in the order they appear. Standard procedure is to layout your html like so (a few fields removed for brevity);

<td><td><input name=timestart><input name=timeend><input name=tasks></td></tr>
<td><td><input name=timestart><input name=timeend><input name=tasks></td></tr>
<td><td><input name=timestart><input name=timeend><input name=tasks></td></tr>
... etc

这将为您提供一个帖子结构,例如;

This will give you a post structure such as ;

Array
(
[timestart] => Array
    (
        [0] => timestart 1
        [1] => timestart 2
        [2] => timestart 3
    )

[timeend] => Array
    (
        [0] => timeend 1
        [1] => timeend 2
        [2] => timeend 3
    )

[task] => Array
    (
        [0] => task 1
        [1] => task 2
        [2] => task 3
));

并插入;

$timestart = $_POST['timestart'];
$timeend = $_POST['timeend'];
$task = $_POST['task'];

for($i=1 ; $i < count($timestart) ; $i++)
{
    $sql = "INSERT INTO TSTable VALUES($timestart[$i],$timeend[$i],$task[$i]);"
    ... more code goes here..
}