我試圖刪除數據庫中的多個條目。我正在使用複選框來確定需要刪除的數據。一旦我勾選了複選框,它會向我發送我用來刪除它的主鍵的值。到目前爲止,我想出了這個使用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