PHP动态数据库插入
我有一个表单,其中有一行名为PHP动态数据库插入,php,javascript,mysql,post,Php,Javascript,Mysql,Post,我有一个表单,其中有一行名为target1,还有一个JavaScript,它可以动态添加行,即target2、target3等,对于策略和资源也是如此 我想使用php和mysql将它们输入数据库。由于提交行的数量是动态的,我想我需要一个foreach语句或while循环 我尝试了一次失败的尝试 这是代码 动态行JavaScript/PHP文件: <?php header('Content-type: application/x-javascript'); if($_REQUEST['typ
target1
,还有一个JavaScript,它可以动态添加行,即target2
、target3
等,对于策略
和资源
也是如此
我想使用php和mysql将它们输入数据库。由于提交行的数量是动态的,我想我需要一个foreach
语句或while
循环
我尝试了一次失败的尝试
这是代码
动态行JavaScript/PHP文件:
<?php
header('Content-type: application/x-javascript');
if($_REQUEST['type'] == "targets"){
?>
function addRowToTargetTable()
{
var tbl = document.getElementById('targets');
var lastRow = tbl.rows.length;
// if there's no header row in the table, then iteration = lastRow + 1
var iteration = lastRow;
var row = tbl.insertRow(lastRow);
// left cell
var cellLeft = row.insertCell(0);
var textNode = document.createTextNode(iteration + ".");
var targetid = document.createElement('input');
targetid.type = 'hidden';
targetid.name = 'target' + iteration;
targetid.id = 'target' + iteration;
targetid.value = 'target' + iteration;
cellLeft.appendChild(textNode);
cellLeft.appendChild(targetid);
// right cell
var cellRight = row.insertCell(1);
var el = document.createElement('textarea');
el.name = 'target' + iteration;
el.id = 'target' + iteration;
el.className = 'validate[required]';
el.style.cssText="margin:4px; max-width:400px; width:400px; max-height:35px; height:35px;";
cellRight.appendChild(el);
// far right cell
var cellFarRight = row.insertCell(2);
var el2 = document.createElement('textarea');
el2.name = 'targetcriteria' + iteration;
el2.id = 'targetcriteria' + iteration;
el2.className = 'validate[required]';
el2.style.cssText="margin:4px; max-width:400px; width:400px; max-height:35px; height:35px;";
cellFarRight.appendChild(el2);
}
function removeRowFromTargetTable()
{
var tbl = document.getElementById('targets');
var lastRow = tbl.rows.length;
if (lastRow > 2) tbl.deleteRow(lastRow - 1);
}
<?php
}elseif($_REQUEST['type'] == "strategies"){
?>
function addRowToStrategyTable()
{
var tbl = document.getElementById('strategies');
var lastRow = tbl.rows.length;
// if there's no header row in the table, then iteration = lastRow + 1
var iteration = lastRow;
var row = tbl.insertRow(lastRow);
// left cell
var cellLeft = row.insertCell(0);
var textNode = document.createTextNode(iteration + ".");
var strategyid = document.createElement('input');
strategyid.type = 'hidden';
strategyid.name = 'strategy' + iteration;
strategyid.id = 'strategy' + iteration;
strategyid.value = 'strategy' + iteration;
cellLeft.appendChild(textNode);
cellLeft.appendChild(strategyid);
// right cell
var cellRight = row.insertCell(1);
var el = document.createElement('textarea');
el.name = 'strategy' + iteration;
el.id = 'strategy' + iteration;
el.className = 'validate[required]';
el.style.cssText="margin:4px; max-width:400px; width:400px; max-height:35px; height:35px;";
cellRight.appendChild(el);
}
function removeRowFromStrategyTable()
{
var tbl = document.getElementById('strategies');
var lastRow = tbl.rows.length;
if (lastRow > 2) tbl.deleteRow(lastRow - 1);
}
<?php }elseif($_REQUEST['type'] == "resources"){
?>
function addRowToResourceTable()
{
var tbl = document.getElementById('resources');
var lastRow = tbl.rows.length;
// if there's no header row in the table, then iteration = lastRow + 1
var iteration = lastRow;
var row = tbl.insertRow(lastRow);
// left cell
var cellLeft = row.insertCell(0);
var textNode = document.createTextNode(iteration + ".");
var resourceid = document.createElement('input');
resourceid.type = 'hidden';
resourceid.name = 'resource' + iteration;
resourceid.id = 'resource' + iteration;
resourceid.value = 'resource' + iteration;
cellLeft.appendChild(textNode);
cellLeft.appendChild(resourceid);
// right cell
var cellRight = row.insertCell(1);
var el = document.createElement('textarea');
el.name = 'resource' + iteration;
el.id = 'resource' + iteration
el.style.cssText="margin:4px; max-width:400px; width:400px; max-height:35px; height:35px;";
cellRight.appendChild(el);
}
function removeRowFromResourceTable()
{
var tbl = document.getElementById('resources');
var lastRow = tbl.rows.length;
if (lastRow > 2) tbl.deleteRow(lastRow - 1);
}
<?php
}
?>
函数addRowtTargetTable()
{
var tbl=document.getElementById('targets');
var lastRow=tbl.rows.length;
//如果表中没有标题行,则迭代=lastRow+1
var迭代=最后一行;
变量行=tbl.insertRow(最后一行);
//左室
var cellLeft=行插入单元格(0);
var textNode=document.createTextNode(迭代+”);
var targetid=document.createElement('input');
targetid.type='hidden';
targetid.name='target'+迭代;
targetid='target'+迭代;
targetid.value='target'+迭代;
cellLeft.appendChild(textNode);
左单元格。追加子对象(targetid);
//右室
var cellRight=行insertCell(1);
var el=document.createElement('textarea');
el.name='目标'+迭代;
el.id='目标'+迭代;
el.className='validate[required]';
el.style.cssText=“边距:4px;最大宽度:400px;宽度:400px;最大高度:35px;高度:35px;”;
cellRight.appendChild(el);
//极右细胞
var cellFarRight=行insertCell(2);
var el2=document.createElement('textarea');
el2.name='targetcriteria'+迭代;
el2.id='targetcriteria'+迭代;
el2.className='validate[required]';
el2.style.cssText=“边距:4px;最大宽度:400px;宽度:400px;最大高度:35px;高度:35px;”;
cellFarRight.appendChild(el2);
}
函数removeRowFromTargetTable()
{
var tbl=document.getElementById('targets');
var lastRow=tbl.rows.length;
如果(lastRow>2)tbl.deleteRow(lastRow-1);
}
函数addRowToStrategyTable()
{
var tbl=document.getElementById('strategies');
var lastRow=tbl.rows.length;
//如果表中没有标题行,则迭代=lastRow+1
var迭代=最后一行;
变量行=tbl.insertRow(最后一行);
//左室
var cellLeft=行插入单元格(0);
var textNode=document.createTextNode(迭代+”);
var strategyid=document.createElement('input');
strategyid.type='hidden';
strategyid.name='strategy'+迭代;
strategyid.id='strategy'+迭代;
strategyid.value='strategy'+迭代;
cellLeft.appendChild(textNode);
左室追加子体(策略ID);
//右室
var cellRight=行insertCell(1);
var el=document.createElement('textarea');
el.name=‘策略’+迭代;
el.id=‘策略’+迭代;
el.className='validate[required]';
el.style.cssText=“边距:4px;最大宽度:400px;宽度:400px;最大高度:35px;高度:35px;”;
cellRight.appendChild(el);
}
函数removeRowFromStrategyTable()
{
var tbl=document.getElementById('strategies');
var lastRow=tbl.rows.length;
如果(lastRow>2)tbl.deleteRow(lastRow-1);
}
函数addRowToResourceTable()
{
var tbl=document.getElementById('resources');
var lastRow=tbl.rows.length;
//如果表中没有标题行,则迭代=lastRow+1
var迭代=最后一行;
变量行=tbl.insertRow(最后一行);
//左室
var cellLeft=行插入单元格(0);
var textNode=document.createTextNode(迭代+”);
var resourceid=document.createElement('input');
resourceid.type='hidden';
resourceid.name='resource'+迭代;
resourceid.id='resource'+迭代;
resourceid.value='resource'+迭代;
cellLeft.appendChild(textNode);
cellLeft.appendChild(resourceid);
//右室
var cellRight=行insertCell(1);
var el=document.createElement('textarea');
el.name=‘资源’+迭代;
el.id='资源'+迭代
el.style.cssText=“边距:4px;最大宽度:400px;宽度:400px;最大高度:35px;高度:35px;”;
cellRight.appendChild(el);
}
函数removeRowFromResourceTable()
{
var tbl=document.getElementById('resources');
var lastRow=tbl.rows.length;
如果(lastRow>2)tbl.deleteRow(lastRow-1);
}
表格如下:
<div id="non-indent">
<form action="complete-iep-operations.php?operation=create&action=insert" method="post" id="validate" enctype="multipart/form-data">
<table border="1" class="listing full" id="register" style="border-collapse:collapse;">
<tr class="no-pointer">
<th scope="col" width="48">
<h4>IEP No.</h4>
</th>
<th scope="col" width="153">
<h4>IEP Date</h4>
</th>
<th scope="col" width="124">
<h4>Renewal Date</h4>
</th>
<th scope="col" width="116">
<h4>IEP Meeting Conducted</h4>
</th>
</tr>
<tr>
<td>
<p><input name="iep_number" type="text" id="iep_number" maxlength="2" class="validate[required]" style="margin-top:5px; width:40px;" /></p>
</td>
<td>
<p>
<input type="text" style="margin-top:5px; width:30px;" class="validate[required]" name="iep-day" id="iep-day" maxlength="2" />
-
<input type="text" style="margin-top:5px; width:30px;" class="validate[required]" name="iep-month" id="iep-day" maxlength="2" />
-
<input type="text" style="margin-top:5px; width:60px;" class="validate[required]" name="iep-year" id="iep-day" maxlength="4" />
</p>
</td>
<td>
<p>
<input type="text" style="margin-top:5px; width:30px;" class="validate[required]" name="renewal-day" id="renewal-day" maxlength="2" />
-
<input type="text" style="margin-top:5px; width:30px;" class="validate[required]" name="renewal-month" id="renewal-day" maxlength="2" />
-
<input type="text" style="margin-top:5px; width:60px;" class="validate[required]" name="renewal-year" id="renewal-day" maxlength="4" />
</p>
</td>
<td>
<p>
<input type="text" style="margin-top:5px; width:30px;" class="validate[required]" name="completed-day3" id="completed-day3" maxlength="2" />
-
<input type="text" style="margin-top:5px; width:30px;" class="validate[required]" name="completed-day3" id="completed-day3" maxlength="2" />
-
<input type="text" style="margin-top:5px; width:60px;" class="validate[required]" name="completed-day3" id="completed-day3" maxlength="4" />
</p>
</td>
</tr>
</table>
<br />
<h3>Targets</h3>
<br />
<table border="1" class="listing full" id="targets" style="border-collapse:collapse;">
<tr class="no-pointer">
<th scope="col" width="102">
<h4>Target Number</h4>
</th>
<th scope="col">
<h4>Target Details</h4>
</th>
<th scope="col">
<h4>Target Achievement Criteria</h4>
</th>
</tr>
<tr>
<td>
<p>1.</p>
<input type="hidden" value="target1" id="target1" />
</td>
<td>
<label for="target1"></label>
<textarea name="target1" id="target1" class="validate[required]" style="margin:4px; max-width:400px; width:400px; max-height:35px; height:35px;"></textarea>
</td>
<td>
<label for="targetcriteria1"></label>
<textarea name="targetcriteria1" id="targetcriteria1" class="validate[required]" style="margin:4px; max-width:400px; width:400px; max-height:35px; height:35px;"></textarea>
</td>
</tr>
</table>
<br />
<table border="1" class="listing full" id="register" style="border-collapse:collapse;">
<tr>
<td>
<div align="right">
<input type="button" value="Remove Target Row" id="submit" onclick="removeRowFromTargetTable();" style="margin:0px; margin-right:10px; float:right;" />
<input type="button" value="Add Another Target" id="submit" onclick="addRowToTargetTable();" style="margin:0px; margin-right:20px; float:right;" />
</div>
</td>
</tr>
</table>
<br />
<h3>Strategies</h3>
<br />
<table border="1" class="listing full" id="strategies" style="border-collapse:collapse;">
<tr class="no-pointer">
<th scope="col" width="102">
<h4>Strategy Number</h4>
</th>
<th scope="col">
<h4>Strategy Details</h4>
</th>
</tr>
<tr>
<td>
<p>1.</p>
<input type="hidden" value="strategy1" id="strategy1" />
</td>
<td>
<label for="strategy1"></label>
<textarea name="strategy1" id="strategy1" class="validate[required]" style="margin:4px; max-width:400px; width:400px; max-height:35px; height:35px;"></textarea>
</td>
</tr>
</table>
<br />
<table border="1" class="listing full" id="register" style="border-collapse:collapse;">
<tr>
<td>
<div align="right">
<input type="button" value="Remove Strategy Row" id="submit" onclick="removeRowFromStrategyTable();" style="margin:0px; margin-right:10px; float:right;" />
<input type="button" value="Add Another Strategy" id="submit" onclick="addRowToStrategyTable();" style="margin:0px; margin-right:20px; float:right;" />
</div>
</td>
</tr>
</table>
<br />
<h3>Resources</h3>
<br />
<table border="1" class="listing full" id="resources" style="border-collapse:collapse;">
<tr class="no-pointer">
<th scope="col" width="102">
<h4>Resource Number</h4>
</th>
<th scope="col">
<h4>Resource Details</h4>
</th>
</tr>
<tr>
<td>
<p>1.</p>
<input type="hidden" value="resource1" id="resource1" />
</td>
<td>
<label for="resource1"></label>
<textarea name="resource1" id="resource1" style="margin:4px; max-width:400px; width:400px; max-height:35px; height:35px;"></textarea>
</td>
</tr>
</table>
<br />
<table border="1" class="listing full" id="register" style="border-collapse:collapse;">
<tr>
<td>
<div align="right">
<input type="button" value="Remove Resource Row" id="submit" onclick="removeRowFromResourceTable();" style="margin:0px; margin-right:10px; float:right;" />
<input type="button" value="Add Another Resource" id="submit" onclick="addRowToResourceTable();" style="margin:0px; margin-right:20px; float:right;" />
</div>
</td>
</tr>
</table>
<br />
<table border="0" class="full" id="register" style="border-collapse:collapse;">
<tr>
<td>
<div align="right">
<input name="submit" type="submit" id="submit" style="margin:0px; margin-right:10px; float:right;" /></div>
</td>
</tr>
</table>
</form>
</div>
IEP号。
IEP日期
续约日期
独立专家小组举行会议
-
-
-
-
-
-
目标
目标号码
目标详细信息
目标实现标准
一,
策略
战略编号
战略细节
一,
资源
资源编号
资源详细信息
一,
以及php代码:
<?php
mysql_connect('host', 'XXXXX', 'XXXXX') or die(mysql_error());
mysql_select_db('db401728075') or die(mysql_error());
if($_REQUEST['operation']=="create" && $_REQUEST['action']=="insert") {
$targeti = 1;
while(true) {
if(isset($_POST['target'.$targeti])) {
$target = $_POST['target'.$targeti];
echo $target;
echo "<BR>";
$targeti++;
} else {
break;
}
}
在php代码中,您没有执行任何mysql插入操作。同时$targeti=“2”
应该类似于$targeti++
否则您总是循环使用target2
您不能迭代$\u POST['target'],因为它不存在。你需要这样的东西:
$targeti = 1;
while(true) {
if(isset($_POST['target'.$targeti])) {
$target = $_POST['target'.$targeti];
// process $target value here
$targeti++;
} else {
break;
}
}
其他类型的代码也一样
已编辑:如果不想在循环中处理$target的值,可以将其保存在数组中($targets[]=$target)。在循环中处理它或在数组中保存它的主要思想是,如果您要动态创建变量(${'target'.$targeti}=$\u POST[…]),那么您需要保存计数器变量($targeti),并在以后使用它来查找所有创建的变量,而这样的代码并不清楚。
<textarea name="target []" id="target" class="validate[required]" style="margin:4px; max-width:400px; width:400px; max-height:35px; height:35px;"></textarea>
<?php
if(isset($_POST['target'])) {
$target = $_POST['target'];
foreach($target as $targetvalue){
echo $targetvalue; // do something with the target value here.
}
}
?>