为什么我的表单会删除最后一行而不是选中的一行?

时间:2016-08-01 09:16:40

标签: php mysql mysqli

在我的代码中,它首先显示表的结果,然后它将在提交时删除数据。但是有问题,无论我在任何地方点击删除按钮,我都会删除最后一行。它不会删除我要删除的特定行

$servername = "localhost";
$username = "root";
$password = "1234";
$db = "ptcl";
$con = new mysqli($servername, $username, $password, $db);

结束连接

html代码

  <html>
 <body>
<div class="content">
<div class="exchange">
    <center>EXCHANGE /NTE</center>>
<center>
<form action="delete.php" name="delete" method="POST">
<table border="1">
<tr><th rowspan="2">EXCHANGE TYPE</th>
    <th colspan="9">DESCRIPTION</th>
</tr>
<tr>
    <td colspan="2">Retail</td>
    <td>DTEs</td>
    <td>Retail/Sett</td>
    <td>Online</td>
    <td>Offline</td>
    <td>Total</td>
    <td colspan="2">Action</td>
</tr>

我希望先显示的表单然后按其ID

删除
    <?php   
    global $con;
    $query="select * from exchange ";
    $result =$con->query($query);

    if($result->num_rows > 0){


while($row = $result->fetch_assoc()){?>

<tr>
    <td><?php echo $row['exchangetype']?></td>
        <td><?php echo $row['retail']?><td>
     <td ><?php echo $row['dte']?></td>
    <td><?php echo $row['retail_sett']?></td>
    <td><?php echo $row['retail']+$row['dte']+$row['retail_sett']?></td>
    <td><?php echo $row['offine']?></td>
    <td><?php echo $row['retail']+$row['dte']+$row['retail_sett']+$row['offine']?></td>
    <td><input type="submit" name="delete" value="Delete"/></td>
          <td><input type="hidden" name="eid" value="<?php echo $row['eid']?>" />  
    </td>
    <?php } 
    ?>
</tr>
</table>
</form>
    </center>
 <?php  } else{
echo "No record found";
        }

   ?>       
</div>
   </div>

php删除代码启动

global $con

$eeid = $_POST['eid'];

if (isset($_POST['delete'])) {
    $query = "DELETE FROM exchange WHERE eid='$eeid' ";

    if ($con->query($query) === true) {
        echo "DELETED Data";
    } else {
        echo "error during deletion" . $con->error;
    }
}

它不会删除我要删除的内容只删除最后一行。

2 个答案:

答案 0 :(得分:1)

问题是你在表单中重复隐藏的输入,并且它们都具有相同的名称。提交表单时,$_POST['eid']将只是最后一个。

您需要为每一行创建一个单独的表单,而不是整个表格的一个表单,以便提交按钮只提交一个eid

while($row = $result->fetch_assoc()){?>

<tr>
    <td><?php echo $row['exchangetype']?></td>
        <td><?php echo $row['retail']?><td>
     <td ><?php echo $row['dte']?></td>
    <td><?php echo $row['retail_sett']?></td>
    <td><?php echo $row['retail']+$row['dte']+$row['retail_sett']?></td>
    <td><?php echo $row['offine']?></td>
    <td><?php echo $row['retail']+$row['dte']+$row['retail_sett']+$row['offine']?></td>
    <td><form method='post' action="delete.php">
            <input type="submit" name="delete" value="Delete"/>
            <input type="hidden" name="eid" value="<?php echo $row['eid']?>" />  
        </form></td>
    <?php } 

另一个选择是将EID放入提交按钮的值,而不是隐藏字段。

<td><input type="submit" name="delete" value="<?php echo $row['eid'] ?>">/</td>

然后delete.php会这样做:

$eeid = $_POST['delete'];

答案 1 :(得分:0)

您的代码问题是您使用它的隐藏类型总是超过写旧值并将您的最后一个值设置为其值 使用GET方法来克服这个问题

<td><a href="delete.php?row_id=<?php echo $row['eid']?>" /></a></td>  
 </td>

在delete.php或任何文件名中使用此代码

//mysql_connection
if(isset($_GET['row_id'])){
 $eeid=$_GET['row_id'];
 $query="delete from exchange
       where eid='$eeid' ";

if ($con->query($query)===TRUE){
  echo "DELETED Data";
  }
  else{
  echo "error during deletion".$con->error;
  }
 }
}