0

伙计们,我有以下脚本用于在 mysql 中添加、编辑和删除内容,并在 index.php 文件中显示它。所以这是我的 index.php 文件:

<script>function goDel() 
{ 
    var recslen =  document.forms[0].length; 
    var checkboxes="" 
    for(i=1;i<recslen;i++) 
    { 
        if(document.forms[0].elements[i].checked==true) 
        checkboxes+= " " + document.forms[0].elements[i].name 
    } 

    if(checkboxes.length>0) 
    { 
        var con=confirm("Are you sure you want to delete"); 
        if(con) 
        { 
            document.forms[0].action="delete.php?recsno="+checkboxes 
            document.forms[0].submit() 
        } 
    } 
    else 
    { 
        alert("No record is selected.") 
    } 
} 

function selectall() 
{ 
//        var formname=document.getElementById(formname); 

        var recslen = document.forms[0].length; 

        if(document.forms[0].topcheckbox.checked==true) 
            { 
                for(i=1;i<recslen;i++) { 
                document.forms[0].elements[i].checked=true; 
                } 
    } 
    else 
    { 
        for(i=1;i<recslen;i++) 
        document.forms[0].elements[i].checked=false; 
    } 
} 
</script>
<style type="text/css"> 
<!-- 
.style1 {color: #FFFFFF} 
--> 
</style> 
</head> 

<body> 
<table width="775" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF"> 
  <tr> 
    <td><hr size="1" noshade></td> 
  </tr> 
  <tr> 
    <td> 
      <form action="" method="post" name="" id=""> 
        <table width="600" border="1" align="center" cellpadding="2" cellspacing="2"> 
          <tr> 
            <td><input name="topcheckbox" type="checkbox" class="check" id="topcheckbox" onClick="selectall();" value="ON"> 
Select All    </td> 
            <td colspan="3" align="center"><a href="form.php?mode=add">Add New Branch </a></td> 
          </tr> 
          <tr> 
            <td><strong><a href="javascript:goDel()">Delete</a></strong></td> 
            <td><strong>Branch Name </strong></td> 
            <td><strong>Short Name </strong></td> 
            <td><strong>Update</strong></td> 
          </tr> 
          <? 
          include("conn.php"); 
          $sql="select sn,branchname,shortname from $branch order by sn"; 
          $result=mysql_query($sql,$connection) or die(mysql_error()); 
          while($row=mysql_fetch_array($result)) { 
          ?> 
          <tr> 
            <td><input name="<? echo $row['sn']; ?>" type="checkbox" class="check"></td> 
            <td><? echo $row['branchname']; ?></td> 
            <td><? echo $row['shortname']; ?></td> 
            <td><a href="<? echo "form.php?sn=".$row['sn']."&mode=update"; ?>">Update</a></td> 
          </tr> 
          <? } ?> 
        </table> 
    </form></td> 
  </tr> 
</table> 

这是我的 delete.php 文件:

<?php 
include("conn.php"); 
$recsno=$_GET["recsno"]; 
$data=trim($recsno); 
$ex=explode(" ",$data); 
$size=sizeof($ex); 
for($i=0;$i<$size;$i++) { 
    $id=trim($ex[$i]); 
    $sql="delete from $branch where sn='$id'"; 
    $result=mysql_query($sql,$connection) or die(mysql_error()); 

} 
header("location: index.php"); 
?>

问题是当我检查 1 行并单击删除时,它正在删除所有行,就像我单击全选(我没有这样做)一样。先感谢您。

4

1 回答 1

0

似乎删除查询没有获得 的值$id,您必须将值存储在$id.

只是获取id

$id = $_REQUEST["sn"]; 

那么你的delete query

$sql="delete from $branch where sn='$id'";
于 2013-03-26T18:41:31.620 回答