我试图删除数据库中的多个条目。我正在使用复选框来确定需要删除的数据。一旦我勾选了复选框,它会向我发送我用来删除它的主键的值。到目前为止,我想出了这个使用PDO,PHP和MYSQL使用primaryKey和复选框删除语句
当我尝试删除单个或多个数据,它直接到成功页面,成功地计算删除玩家的预期量的数量。但是当我显示所有表格时没有任何反应。
HTML/PHP代码
<?php
$self = htmlentities($_SERVER['PHP_SELF']);
echo "<form id='delete' method='POST' action='$self'>";
?>
<div class="content">
<h3>Delete Athlete</h3>
<hr>
<table>
<tr>
<th>Select Player</th><th>Photo</th><th>First Name</th><th>Last Name</th><th>Gender</th><th>Sport</th><th>Country</th><th>Population</th><th>Flag</th>
</tr>
<?php
foreach($showResult as $row)
{
echo("<tr><td><input type='checkbox' name='checkdelete[]' value='$row[athleteID]'>$row[athleteID]</td>
<td><img class='photo' src='files/$row[image]' alt='$row[lastName].jpg'/></td>
<td>$row[firstName]</td><td>$row[lastName]</td>
<td><img class='genderImg' src='files/$row[gender].png' alt='$row[gender].jpg'/></td>
<td>$row[sport]</td><td>$row[countryName]</td><td>$row[population]</td>
<td><img class = 'flag' src='files/flags/$row[flag]' alt='$row[flag].jpg'/></tr>");
}
?>
</table>
<br><input type="submit" name="deleteAth" value="Delete">
</div>
</body>
,我有此代码为PHP
<?php
包括 'connect.inc.php';
try // select statement for the output page/delete page
{
$showJoinedTbl = "SELECT athleteID,image, firstName, lastName, gender, sport,
countryName, population, flag from athTbl join
counTbl on athTbl.countryID = counTbl.countryID ORDER by firstName";
$showResult = $pdo->query($showJoinedTbl);
}
catch (PDOException $e)
{
$error = 'Select statement error';
include 'error.html.php';
exit();
}
$dataCorrect = true;
if (isset($_POST['deleteAth']))
{
try
{
$valueID = $_POST['checkdelete'];
$N = count ($valueID);
for ($i=0; $i; $i++)
{
$deleteQry = $db->prepare("DELETE from athTbl WHERE athleteID= ?");
echo ("$valueID[$i]");
//$stmt = $pdo->prepare($deleteQry);
$stmt->bindValue(1,$valueID[$i]);
$stmt->execute();
} //when I try to delete single or multiple data, it goes straight to success page which successfully count the number of intended amount of deleted player. But nothing happens when I show all tables.
}
catch (PDOException $e)
{
$error = 'Error deleting data from athleteTbl';
include 'error.html.php';
exit();
}
include 'DeleteSuccess.html.php'; // output screen if I deleted it successfully.
}
else
{
include 'Delete.html.php';
}
?>
形式是HTML/PHP。显示在Delete.html.php –
中的显示每个运动员ID的checkBox的$ showResult首先,执行select操作,然后处理删除操作。因此,选定的结果仍然会包含要删除的记录,因为删除语句尚未运行。 – Shadow