I'm trying to update/delete a SQL row based on the value of a radio button using PHP.
My code looks like the below and so far, when i click the submit button - it does nothing!
<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>";
}
?>
There are some other bits of code in this however, it seems like the button does nothing at the moment and I'm really not too sure why. I am new to PHP, so go easy please :)
If anyone can see that this is going to go wrong in other ways, please let me know as I haven't been able to test it yet as the button won't work.
Successful!
"; } ?> – TechyChick94 Sep 13 '17 at 23:28