简体   繁体   中英

Update MySQL Database based on option selected from an HTML dropdown menu

I'm working on a website for Pokemon Go, and as part of the site I'm making a custom CMS that allows me to insert/alter data for displaying on various webpages. I want to be able to toggle a Raid Boss to "inactive" based on the boss selected in a drop down menu.

The following is the HTML code for the form where the dropdown is located. Basically I query the database of Raid Bosses for all bosses that are currently active, or "1", and append them to the option list:

<h5 style="text-decoration: underline;">Set Active Raid Boss to Inactive</h5>
    <form class = "form-group" method = "post">
          <select>
          <option disabled selected value> -- select an option -- </option>
          <?php
               $prep_stmt = $conn->prepare("SELECT * FROM raids WHERE isActive = 1");
               $prep_stmt->execute();
               $row = $prep_stmt->fetchAll();
               $count = $prep_stmt->rowCount();

               for($x = 0; $x < $count; $x++) {
                    echo "<option name ='".$row[$x]['name']."' value='".$row[$x]['name']."'>". $row[$x]['name']. "</option>";
               }
          ?>
          </select>
          <input name = "ToggleToInactiveRaid" type = "submit" value="Submit"/>
   </form>

The following is the PHP code for the $_POST request when the button is clicked:

if(isset($_POST['ToggleToInActiveRaid'])){
    $raids = $conn->prepare("SELECT * FROM raids WHERE isActive = 1");
    $raids->execute();

    $raidList = $raids->fetchAll();
    $count = $raids->rowCount();

    for($i = 0; $i < $count; $i++){
        if ($raidList[$i]['name'] == $_POST['name']){
            $stmt = $conn->prepare("UPDATE raids SET isActive = 0 WHERE raids.name = ".$_POST['name']);
            $stmt->execute();
        }
    }
}

For some reason, the POST request is never detected. I know this because I was echoing a dummy variable in the form section that would only be displayed if its value was updated in the POST request if block, and that variable was never dumped onto the page. I have other forms on the CMS page where the PHP code is executed when the respective button is clicked. But this one is giving me quite a bit of trouble and I really don't know why. If anyone can help me out, that would be awesome. Thanks!

A) You didn't give your select-box a name. So it won't show up in your $_POST -array. Change <select> to <select name="name">

B) (Assuming isActive can only be 0 or 1 ) you don't have to loop over all the rows in the database to de-activate just one. Just use (fixed it with prepared statement)

$stmt = $conn->prepare("UPDATE raids SET isActive = 0 WHERE raids.name = :name");
$stmt->execute(array(':name' => $_POST['name']));

It will do the same either with or without your SELECT * FROM raids WHERE isActive = 1; for(...) SELECT * FROM raids WHERE isActive = 1; for(...) . In both cases either that name will exists in the raids-table (and be updated to 0 ) or doesn't exists (and nothing will be updated). Skipping the SELECT, for(...) will just make your code more efficient.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM