我正在尝试删除数据库中的多个条目。我正在使用复选框来确定我需要删除哪些数据。勾选复选框后,它会向我发送我用来删除它的主键值。到目前为止,我已经提出了这个
当我尝试删除单个或多个数据时,它直接进入成功页面,成功计算已删除玩家的预期数量。但是当我显示所有表格时没有任何反应。
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';
}
?>
答案 0 :(得分:0)
你的循环中有错误:
...
$N = count ($valueID);
for ($i=0; $i; $i++)
^^ 0 on the first iteration
{
...
当条件(中间值)为true
时,您的循环将运行。您将$i
设置为0,其值为false
,因此您的循环永远不会运行。
或者,来自manual:
在每次迭代开始时,将评估expr2。如果它 计算结果为TRUE,循环继续,嵌套语句为 执行。如果计算结果为FALSE,则循环的执行结束。
你可能想要:
...
$N = count ($valueID);
for ($i=0; $i < $N; $i++)
{
...