2015-11-02 88 views
1

这是我的动态表格行的html代码。这通过单击添加新按钮来复制表格字段。问题是我无法将所有填充的数据插入到数据库中。如果你能帮助我,那会很好。非常感谢。如何将动态表格行数据一次性插入到数据库中

<!-- 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

<?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查询部分需要修复。


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); 
} 
} 

    } 
+0

http://stackoverflow.com/questions/11483799/php-mysql-dynamic-prepared-statement-with-insert-update-query –

回答

0

这里有几件事情,会让你的生活更容易,如果改变了。

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

<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 

这会给你一个帖子结构,如;

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

嗨。还是行不通。但感谢您的答案。 :D – Sam

+0

何时'具有相同名称的输入元素将按照它们出现的顺序作为数组提交。真的吗? – chris85

+1

@ chris85这就是当有人试图使用不起作用的代码时会发生什么,呃? ;-) –

相关问题