如何将动态表行数据立即插入数据库 [英] How to insert the dynamic table row data into database at once

查看:130
本文介绍了如何将动态表行数据立即插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的动态表行的html代码.通过单击添加新按钮,这将复制表字段.问题是我无法将所有填充的数据插入数据库.如果您能帮助我,那就太好了.非常感谢.

This is my html codes for dynamic table rows. This duplicate the table fields by clicking the Add new button. The problem is I cannot insert all the filled data into the database. It would be nice if you can help me. Thanks a lot.

 <!-- Time&Task -->

                    <div class="box-body">
                    <div class="form-group">
                      <label class="col-sm-2 control-label"></label>
                      <div class="col-sm-10">

                       <br>
        <table class="table table-striped" id="maintable" width="50%" cellpadding="0" cellspacing="0" class="pdzn_tbl1" border="0px">
        <tr>
                <th>Time Start:</th>
                <th>Time End:</th>
                <th>Task:</th>
                <th>Comment:</th>
        </tr>

        <tr class="rows"> 
            <td style="padding:5px;">
                <input type="time" name="item[0][timestart]" />
            </td>
            <td style="padding:5px;">
                <input type="time" name="item[0][timeend]" />
            </td>
            <td style="padding:5px;">
                <input type="text" name="item[0][tasks]" />
            </td>
            <td style="padding:5px;">
                <input type="text" name="item[0][comment]" />
            </td>
        </tr>

    </table>
    <div id="add_new">ADD NEW</div>


    <?php
if (isset($_POST['item']) && is_array($_POST['item'])) {
    $items = $_POST['item'];
    $i = 0;
    foreach($items as $key => $value) {
        echo 'Item '.$i++.': '.$value['timestart'].' '.$value['timeend'].' '.$value['tasks'].' '.value['comment'].'<br />';
    }
}
?>

                      </div>
                    </div>
                  </div><!-- /.box-body -->


这是我要插入的sql查询.这是问题所在.我不知道如何循环插入数据库.谢谢. :D


This is my sql query for inserting. here is the problem. I don't know how to loop to insert into the DB. Thanks. :D

<?php
    if (isset($_POST['data']) && !empty($_POST['data'])) {

     $timestart = htmlspecialchars($_POST['timestart']);
     $timeend = htmlspecialchars($_POST['timeend']);
     $tasks = htmlspecialchars($_POST['tasks']);
     $comment = htmlspecialchars($_POST['comment']);

  include('DBConnect.php');


       $SQL = "INSERT INTO TSTable (timestart,timeend,tasks,comment) VALUES ('$timestart','$timeend','$tasks','$comment')";

    if (mysqli_query($conn, $SQL)) {

} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
} 
     }


!大家好,我已经找到了解决方案.这是正确的代码.我希望它可以帮助需要它的人. html仍然相同,只需要修复sql查询部分.


! Hi all, I found a solution already. Here are the correct codes. I hope it helps someone who needs it. The html is still the same and only sql query part needed to fix.

if (isset($_POST['data'])) {

     $timestart = $_POST['timestart'];
     $timeend = $_POST['timeend'];
     $tasks = $_POST['tasks'];
     $comment = $_POST['comment'];

  include('DBConnect.php');

$count= count($timestart);
for ($i=0; $i< $count; $i++){
    if($timestart[$i] != null ||  !empty($timestart[$i])){

       $SQL = "INSERT INTO TSTable (timestart,timeend,tasks,comment) VALUES ( '$timestart[$i]','$timeend[$i]','$tasks[$i]','$comment[$i]')";    
}
if (mysqli_query($conn, $SQL)) {

} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}  

     }

推荐答案

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

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..
}

这篇关于如何将动态表行数据立即插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆