0

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.

Alberto Martinez
  • 2,620
  • 4
  • 25
  • 28
TechyChick94
  • 33
  • 1
  • 8
  • `$count2` is never greater than `0`, it is `0`. – chris85 Sep 13 '17 at 23:12
  • You also have 2 `form`s. – chris85 Sep 13 '17 at 23:13
  • @chris85, thanks for such a quick response. How do I make it so that $count2 = $counter? – TechyChick94 Sep 13 '17 at 23:24
  • You did `$counter = $count2 = "0";` but both are equal to `0`. So this condition `while ($count2 > "0"){` is never met. – chris85 Sep 13 '17 at 23:24
  • Okay, so I changed it to be: "0"){ $CurrentID = $_POST["IDNumber[".$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 "

    Successful!

    "; } ?>
    – TechyChick94 Sep 13 '17 at 23:28
  • Can you please `edit` the question, hard to read code in comments. https://stackoverflow.com/posts/46208117/edit – chris85 Sep 13 '17 at 23:28
  • This now gives me the message "Successful!", but it doesn't seem to actually update or delete the row.. – TechyChick94 Sep 13 '17 at 23:28
  • Sorry. Have updated :) – TechyChick94 Sep 13 '17 at 23:31
  • It looks like you only took out the initialization of `$counter` and `$count2`. Is `$counter` being set earlier? – chris85 Sep 13 '17 at 23:33
  • It's being set in the second while loop - under the SELECT query - which gets executed before if (isset($_GET['action'])) – TechyChick94 Sep 13 '17 at 23:36
  • This is procedural so it executes in the order you have it so you won't enter the first `while`. – chris85 Sep 13 '17 at 23:38
  • Do any errors show from http://php.net/manual/en/function.error-reporting.php and http://php.net/manual/en/pdo.error-handling.php also looking at your developer console? @TechyChick94 also looking at your HTML source to see what's being populated. – Funk Forty Niner Sep 13 '17 at 23:47
  • Okay, I've updated the code again - it doesn't seem to update or delete rows still... – TechyChick94 Sep 13 '17 at 23:47
  • Also, `WHERE (ID=?)` - I'd start by removing those brackets, since those are mostly used for subqueries. – Funk Forty Niner Sep 13 '17 at 23:48
  • I also noticed that you are assiging `=` rather than comparing `==` in a few conditional statements. That alone will make your code fail. I think that the question may be a duplicate of [The 3 different equals](https://stackoverflow.com/questions/2063480/the-3-different-equals) – Funk Forty Niner Sep 13 '17 at 23:49
  • @chris85 Look at what you missed *lol!* ^ – Funk Forty Niner Sep 13 '17 at 23:51
  • @chris85 it doesn't help the cause. It won't enter the `while` because the form wasn't submitted (yet). I won't be submitting an answer, so if you want.... go for it ;-) Edit: You deleted your comment I was responding to here, why? – Funk Forty Niner Sep 13 '17 at 23:55
  • @Fred-ii- Code was updated, now the `count2` actually is set. – chris85 Sep 13 '17 at 23:56
  • @Fred-ii- I've added in error_reporting(E_ALL); and get Notice: Undefined index: IDNumbers[2] and Undefined index: IDNumbers[1] – TechyChick94 Sep 13 '17 at 23:58
  • @TechyChick94 You should put some debugging statements in and see which code blocks execute. There are a few different issues going on here and you're going to need to fall the full execution path to see whats what. I'd also use a form for each field and process each submission individually. – chris85 Sep 13 '17 at 23:58
  • OMG - realised it's 1AM here and I need to be up at 7 - and being a girl, I need my beauty sleep. Will be back on tomorrow guys :) x – TechyChick94 Sep 14 '17 at 00:01
  • @chris85 thank you I will do. I will let you know how it goes tomorrow. – TechyChick94 Sep 14 '17 at 00:02

0 Answers0