我有一些数据来自数据库,我在最后一栏有一个删除按钮。我想删除单击删除按钮的行。但每当我点击删除按钮时,即使我点击第一行,也只删除最后一行。
我的删除功能:
if(isset($_POST['formDelete'])){
if(isset($_POST['quoteid']) && !empty($_POST['quoteid'])){
require_once('config.php');
$conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE)
or die ('Cannot connect to db');
$quoteid = $_POST['quoteid'];
echo "DELETE FROM quotes WHERE quoteid =".$quoteid;
$result = $conn->query("DELETE FROM quotes WHERE quoteid =".$quoteid);
}
表格代码:
<form action="<?php $_PHP_SELF ?>" method="post">
<?php
require_once('config.php');
$conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE)
or die ('Cannot connect to db');
$result = $conn->query("SELECT quoteid, quote, cat.catid catid, cat.name cat, scat.catid catid, scat.name scat, author FROM quotes q
INNER JOIN category cat ON q.catid = cat.catid LEFT OUTER JOIN category scat ON q.subcatid = scat.catid order by quoteid");
echo "<table border='1px solid black'>";
echo "<tr><td><b>Category</b></td><td><b>Sub-Category</b></td><td><b>Quote</b></td><td><b>Author</b></td></tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr><td>$row[cat]</td><td>$row[scat]</td><td><input type='hidden' id='quoteid' name='quoteid' value=$row[quoteid] />$row[quote]</td><td>$row[author]</td>
<td><input type='submit' name='formDelete' id='formDelete' value='Delete' /></td></tr>";
}
echo "</table>";
?>
</form>
我试图在quoteid的基础上删除。
答案 0 :(得分:4)
这个想法是为每个删除按钮创建一个单独的表单,因此quoteid的正确值将被发送
<?php
require_once('config.php');
$conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE)
or die ('Cannot connect to db');
$result = $conn->query("SELECT quoteid, quote, cat.catid catid, cat.name cat, scat.catid catid, scat.name scat, author FROM quotes q
INNER JOIN category cat ON q.catid = cat.catid LEFT OUTER JOIN category scat ON q.subcatid = scat.catid order by quoteid");
?>
<table border='1px solid black'>
<tr>
<td>
<b>Category</b>
</td>
<td>
<b>Sub-Category</b>
</td>
<td>
<b>Quote</b>
</td>
<td>
<b>Author</b>
</td>
</tr>
<?php
while ($row = $result->fetch_assoc()) {
?>
<tr>
<td>
<?php echo $row['cat']; ?>
</td>
<td>
<?php echo $row['scat']; ?>
</td>
<td>
<?php echo $row['quote']; ?>
</td>
<td>
<?php echo $row['author']; ?>
</td>
<td>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method='post' style="display: inline;">
<input type='hidden' id='quoteid' name='quoteid' value="<?php echo $row['quoteid']; ?>" />
<input type='submit' name='formDelete' id='formDelete' value='Delete' />
</form>
</td>
</tr>
<?php
}
?>
</table>