从动态html表向数据库添加数据

时间:2013-11-08 10:44:37

标签: php html mysql

我已经创建了一个动态html表,如下所示:哪里有错误,请帮助我将这些数据添加到MYSQL数据库...我认为for循环中有一个错误,它遍历html表来添加数据,请帮我纠正一下

        *HTML TABLE (two text feilds , i have made thier name as an array task[] and 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 CODE *

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

1 个答案:

答案 0 :(得分: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);
}