我尝试使用PHP根据单选按钮的值更新/删除SQL行。
我的代码如下所示,到目前为止,当我点击提交按钮时 - 它什么也没做!
<form method="post" action="?action">
<?php
$stmt = $pdo->query('SELECT * FROM reviews WHERE Approved="0" ORDER BY id DESC');
while ($result= $stmt->fetch(PDO::FETCH_ASSOC)) {
$reviewtitle= $result['Title'];
$rating= $result['Rating'];
$name= $result['Name'];
$date= $result['Date'];
$review= $result['Review'];
$ID= $result['ID'];
$counter = ++$counter;
?>
<br>
<?php echo $reviewtitle; ?><br>
<?php echo $rating; ?>
<br>
<?php echo $name; ?><br>
<?php echo $review; ?>
<p>Posted: <?php echo $date; ?></p>
<p>Review ID: <input type="text" name="IDNumbers[<?php echo $count; ?>]" value="<?php echo $ID; ?>" readonly></p>
<input type="radio" name="approved[<?php echo $count; ?>]" value="Approve Review"> Approve Review<br>
<input type="radio" name="approved[<?php echo $count; ?>]" value="Delete Review"> Delete Review<br>
</div>
<br>
<?php } ?>
<br>
<br>
<input type="submit" value="Submit">
</form>
-
<?php
$CurrentID = "";
if (isset($_GET['action'])) {
$count2 = $counter;
while ($count2 > "0"){
$CurrentID = $_POST["IDNumbers[".$count2."]"];
if ($_POST["approved[".$count2."]"] = "Approve Review") {
$stmt = $pdo->prepare("UPDATE reviews SET Approved='1' WHERE (ID=?)");
$stmt->execute(array($CurrentID));
} elseif ($_POST["approved[".$count2."]"] = "Delete Review") {
$stmt = $pdo->prepare("DELETE FROM reviews WHERE (ID=?)");
$stmt->execute(array($CurrentID));
}
$count2 = --$count2;
}
echo "<h2> Successful!</h2>";
}
?>
此外还有其他一些代码,看起来按钮此刻什么都不做,我真的不太清楚为什么。我是PHP的新手,所以请轻松一下:)
如果有人可以通过其他方式看到这会出错,请告诉我,因为按钮无法正常工作,我还无法测试它。