在我的代码中,它首先显示表的结果,然后在提交时删除数据。但有一个问题是,无论我在哪里点击删除按钮,我都会删除最后一行。它不会删除我想删除的特定行
$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;
}
}
它不会删除我想删除的内容,只删除最后一行。
问题是在表单中重复隐藏的输入,并且它们都有相同的名称。当您提交表单时,$_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'];
嗨,代码的问题是您使用的隐藏类型,它总是重写旧值并将最后一个值设置为其值使用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;
}
}
}