[英]Adding data to database from dynamic html table
I have created a dynamic html table as follows.我创建了一个动态 html 表,如下所示。
I think there is an error with the for loop which traverses through the html table to add data, please help me correct it.我认为遍历html表添加数据的for循环有错误,请帮我更正。
HTML TABLE (two text fields, I have made their name as an array task[]
and time[]
) HTML TABLE(两个文本字段,我将它们命名为数组
task[]
和time[]
)
<table>
<thead>
<th>PROJECT ↓</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 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);
}
$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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.