0

我创建了一个动态 html 表,如下所示。
我认为遍历html表以添加数据的for循环有错误,请帮我纠正。

HTML TABLE(两个文本字段,我将它们的名称命名为数组task[]time[]

<table>
    <thead>
        <th>PROJECT &#8595;</th>
        <th id="th1" >Time Used (mins)</th>
    </thead>
    <tbody>
        <tr>
            <td><input type="text" name="task[]" id="text2" value=""></td>
            <td><input type="time" name="time[]" value=""></td>
            <td><button type="button" onclick="cloneRow(this)">Add</button></td>
        </tr>
    </tbody>

JAVASCRIPT

<script>        
    // Helper function
    function upTo(el, tagName) {
        el = el && el.parentNode;
        tagName = tagName.toLowerCase();

        for (;el; el = el.parentNode) {
            if (el.tagName && el.tagName.toLowerCase() == tagName) {
                return el;
            }
        }
        return null;
    }

    // Row cloning function
    function cloneRow(el) {
        var newRow;
        var row = el && upTo(el, 'tr');

        if (row) {
            newRow = row.cloneNode(true);
            row.parentNode.appendChild(newRow);
        }
    }
</script>

PHP代码

if (isset($_POST['task']))
{
    $variable_string1 = $_POST['task'];       //task
}
if (isset($_POST['time']))
{
    $variable_string2 = $_POST['time'];         //time
}

foreach ($variable_string1 as $a => $b) { 
    $sql1="INSERT INTO task(e_id,p_name,task,time)VALUES('$val1',  
    $val2,'$variable_string1[a]','$variable_string2[a]')";
    $result=mysql_query($sql1);
}
4

1 回答 1

0
$count = count($variable_string1);
foreach($i = 0; $i < $count; $i++) { 

$sql1="INSERT INTO task(e_id,p_name,task,time)VALUES('$val1',  

      '$val2','".$variable_string1[$i]."','".$variable_string2[$i]."')";
    $result=mysql_query($sql1);
}
于 2013-11-08T10:54:48.630 回答