日期:2014-05-17  浏览次数:20435 次

js动态添加行后,往数据库中插入数据的问题
HTML code
<!--html部分-->

<script language="JavaScript"> 
var cGetRow=-99999;
function insertrow(){
var newrow = document.all.ACE_HIDDEN_TABLE.rows[0].cloneNode(true); //克隆一行
document.all("newTB").appendChild(newrow); //添加刚才克隆的一行
}
function GetRow(){
//获得行索引
//两个parentElement分别是TD和TR,rowIndex是TR的属性
//this.parentElement.parentElement.rowIndex
cGetRow=window.event.srcElement.parentElement.parentElement.rowIndex;


DelRow(cGetRow);//点击checkbox时,直接删除行。
}
function DelRow(iIndex){
//删除一行
if(iIndex==-99999){
alert("系统提示:没有选中行号!");
}else{
newTB.deleteRow(iIndex);
iIndex==-99999;//将rowIndex恢复默认值。
}
}
</script>
<form action="chuli.php" method="post">
<table border="1">
<tr>
    <td width="40">删除</td>
    <td width="40">登记</td>
    <td width="70">用户号码</td>
    <td width="150">建筑名称</td>
</tr>    

 <tbody id="ACE_HIDDEN_TABLE" style="display:none">
  <tr>
    <td width="40"><input type="checkbox" onclick="GetRow()"/></td>
    <td width="40"><input type="text" name="record[]" /></td>
    <td width="70"><input type="text" name="usercode[]" /></td>
    <td width="150"><input type="text" name="buildname[]" /></td>
  </tr>
</table>

<table width="1250" border="1">
<!-- 插入新行的区域 begin -->
<tbody id="newTB">
</tbody>
<!-- 插入新行的区域 end -->
</table>

<div align="center" style="margin:10px;">
<input type="button" onClick="insertrow();" value="增加一行">
<!--<input type="button" onClick="DelRow(cGetRow);" value="删除一行">-->
</div>
<input type="submit" value="保存页面" style="width:120px;height:40px;" name="save" />
</form>


php部分 
PHP code
$con = mysql_connect("localhost","root","mysql") or die ("连接数据库失败"); //数据库用户名及数据库密码
    //数据库名称
    mysql_select_db('BERecord');  //数据库名称
     mysql_query('set names utf8'); 
     
     //只有一行插入时的sql语句
      
        $record=$_POST['record'];                //登记
    //$no=$_POST['no'];                        //NO、
    $usercode=$_POST['usercode'];            //用户号码
    $buildname=$_POST['buildname'];            //建筑名称
    
    for($i=0;$i<3;$i++){
    $sql="insert into record(record,usercode,buildname) values('$record[$i]','$usercode[$i]','$buildname[$i]')";
    $result=mysql_query($sql,$con);