-1

I´m trying to do my graduation exam project, but I got stuck at one step.

I can rate other accounts, this is how it looks on the webpage.

Problem is when i am logged as adminstrator, I want to be allowed to delete the comments, but I dont know how to choose, which one delete input(type=submit) I pressed. Is there any way to determine which one i pressed..?

                $komentare = $spojeni->prepare("SELECT komentare.ID_odesilatele,komentare.ID_prijemce,komentare.Stav,komentare.Nadpis,komentare.Text,uzivatele.Uzivatelske_jmeno FROM komentare JOIN uzivatele ON uzivatele.ID_uzivatele=komentare.ID_odesilatele WHERE ID_prijemce=?");
                $komentare->bind_param("i", $_GET["id_uzivatele"]);
                $komentare->bind_result($komidOd, $komidPr, $komStav, $komNadpis, $komText, $komJmeno);
                $komentare->execute();
                while ($komentare->fetch()) {
                    ?>

                <div class="komentar">
                    <img class="plusminus" src="obr/<?php
                     if ($komStav == 1) {
                         echo "plus.png";
                     }if ($komStav == 0) {
                         echo"minus.png";
                     }
                     ?>" alt="">
                    <div class="nadpisKom">
                        <b><?php echo"$komNadpis"; ?></b>
                    </div>
                    <div class="zaslal">
                        Sent by <a href="ucet.php?id_uzivatele=<?php echo "$komidOd"; ?>"><b><?php echo "$komJmeno"; ?></b></a>
                    </div>
                    <div class="textKom">
<?php echo"$komText" ?>
                    </div>
<?php
if (isset($_SESSION["id_uzivatele"]) && ($_SESSION["opravneni"] > 0 || $_SESSION["id_uzivatele"] == $komidOd)) {
    ?>
                        <input type="submit" name="smazat" value="Delete">
                    <?php
                }
                ?>

                </div>
mario
  • 144,265
  • 20
  • 237
  • 291
  • P.S: Sorry for my english. I´m not a native english speaker... – Adam Zastoupil Feb 17 '18 at 14:26
  • you can use `isset()` and checking if the value for it equals to that of the button. – Funk Forty Niner Feb 17 '18 at 14:28
  • You need a way to discern the buttons then, if all are part of a single form. Typically a hidden field is used to be pass an id along. Otherwise ` – mario Feb 17 '18 at 14:29
  • Possible duplicate of [If an HTML form has two buttons, how do I know which got clicked?](https://stackoverflow.com/questions/2129346/if-an-html-form-has-two-input-type-submit-buttons-how-do-i-know-which-got-c) – Casual Coder Feb 17 '18 at 14:31
  • I know about the if(isset($_Post["smazat){}, but what if I have 2 inputs with same name.. How do i determine which one was clicked? – Adam Zastoupil Feb 17 '18 at 14:36
  • *"but what if I have 2 inputs with same name"* - There is only one button of that name. FYI: `if(isset($_Post` that should be in uppercase `if(isset($_POST` – Funk Forty Niner Feb 17 '18 at 14:38
  • it seems you have only one delete input for all of your action. In order to know what to delete, update your links and point the get value to the actual comment you want to delete. Then query the database to delete that comment. –  Feb 17 '18 at 14:38
  • give them a unique name and use an if statement – Robert Feb 17 '18 at 14:44
  • Possible duplicate of [Two submit buttons in one form](https://stackoverflow.com/questions/547821/two-submit-buttons-in-one-form) – rickdenhaan Feb 17 '18 at 14:52

1 Answers1

0

The best way to do that you will keep the delete button invisible for normal user. to do that you can keep delete button inside a condition.

     if(is_admin($user_id)){
        <input type="submit" name="smazat" value="Delete">
     }else{
         // any thing you wanna show the normal user
     }

in is_admin function you have to check the user is admin or not.

    public function is_admin($id){
         $sql = // your query will be here;
         if($sql->num_rows > 0){
            return true;
         }else{
            return false;
         }
    }
sayalok
  • 882
  • 3
  • 15
  • 30