<!--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部分 
$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);
    
        if($result!= 0){
        echo "数据插入成功";
        echo "<br>";
        echo "正在返回输入页面,跳转中...";
        echo "<meta http-equiv='refresh' content='2;url=yanshi.php'>";  
    }
    
    else
    {
        echo "插入数据失败";
        echo mysql_error();
    }
    }html 用js代码 动态添加行   然后将行里的内容添加到数据库BERecord中
有几个问题  1.插入的循环如何处理?我这里写的固定数,这肯定是不对的,如何根据增加的行数能够自动处理?
                   2.就是添加时,我这里$i=3,输入三个数据,第一个数据是空白数据,这个要如何处理?
                   3.就是我在数据库中,还有一个字段no,用来自增,多次插入后,数据库的顺序就变的混乱了,这是什么原因造成的,谢谢

解决方案 »

  1.   

    1、
    for($i=0;$i<3;$i++){
    改为
    for($i=0;$i<count($record);$i++){2、
    if(trim($record[$i] . $usercode[$i] . $buildname[$i]) == '') continue;
    全为空则跳过。对于其他情况,依计而行3、
    不明白你的意思
      

  2.   

    谢谢 回答追问下  如果按上面的html的话,默认是一行都没有的 这栏 删除 登记 用户号码 建筑名称  也没有底下的线,很难看,能够利用这个默认显示一行 或是如何将那段html美化下? 
      

  3.   

    自己试了一下
    把<tbody id="ACE_HIDDEN_TABLE" style="display:none">
    改成 <tbody id="ACE_HIDDEN_TABLE" style="display:one"> 就变成了默认显示一行
      

  4.   

    但是有个问题,属性为style="display:none"的时候td标签的大小是按我写的 <td width属性控制的
    <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>   点击添加一行后,这行的td标签不受这个控制 就不对齐了
    </table>   改成 style="display:one"之后,td的属性 就不是我限制的大小 (变的特别大),但是上下能够对齐