Making action on all data

1

I'm developing an ad system, where you can submit offers to the ad owner and then approve or reject the offer, if you approve and then negotiate with the offerer.

I came across a problem: When I have only one offer I can approve or reject, but if I have more than one offer when I click on an option it does it for all offers ...

I searched the forum, but I did not find anything like it.

Follow the code (obs: when I threw the code here it messed up the indentation):

            <div class="container2">
                <div class="col-sm-10">
                    <div class="row">
                        <div class="titulo_1">
                        <h1>Ofertas recebidas</h1>
                        </br>
                            </div> 

                                <?php 
                                $id_usuario = $_SESSION["id"];
                                $anuncio = $_GET["id"];

                                $sql = "SELECT * FROM ofertas = OF 
                                INNER JOIN anuncios = AN
                                ON OF.id_anuncio = AN.ID
                                WHERE AN.id_usuario = '$id_usuario' AND OF.status = 'ANALISE'";

                                while($sql = mysql_fetch_array($query2))
                                { ?>

                                    <div class="col-sm-3">
                                    </br>
                                    <div class="text-center"><strong>ID do usuário que enviou a oferta: </strong><?php echo $sql['id_usuario_oferta']; ?></div>
                                    <div class="text-center"><strong>ID da oferta: </strong><?php echo $sql['ID_OF']; ?></div>
                                    <div class="text-center"><strong>ID do anúncio: </strong><?php echo $sql['ID']; ?></div>
                                    <div class="text-center"><strong>Usuário que enviou a oferta: </strong><?php echo $sql['nomeusuario_oferta']; ?></div>
                                    <div class="text-center"><strong>Produto: </strong><?php echo $sql['produto']; ?></div>
                                    <div class="text-center"><strong>Valor oferta: </strong><?php echo $sql['valoroferta']; ?></div>
                                    <div class="text-center"><strong>Valor de recompensa que você informou: </strong><?php echo $sql['valorrecompensa']; ?></div>
                        <div class="text-center"><strong>Viagem que ele irá fazer: </strong><?php echo $sql['viagemcompra']; ?></div>
                        <div class="text-center"><strong>Comentário: </strong><?php echo $sql['comentario']; ?></div>
                        <div class="text-center"><strong>Oferta enviada em: </strong><?php echo $sql['dataoferta']; ?></div>
                        </br>

            <form method="post" action="">
         <button type="submit" name="aceitar" id="aceitar" value="NEGOCIACAO"class="btn btn-success btn-flat">Aceitar</button> 
         <button type="submit" name="rejeitar" id="rejeitar" value="REJEITADO" class="btn btn-danger btn-flat">Rejeitar</button>
     </form>

                   <?php
                $IDoferta = $sql['ID_OF'];
                if(isset($_POST['aceitar'])) {
          $query = "UPDATE ofertas SET status = 'NEGOCIACAO' WHERE ID_OF = '$IDoferta'";
             mysql_query($query);
             echo "<meta http-equiv='refresh' content='0, url=index.php'";
       } ?>

   <?php
         $IDoferta = $sql['ID_OF'];         
        if(isset($_POST['rejeitar'])) {
            $query = "UPDATE ofertas SET status = 'REJEITADO' WHERE ID_OF = '$IDoferta' LIMIT 1";
            mysql_query($query);
            echo "<meta http-equiv='refresh' content='0, url=index.php'";
      } ?>

            </div>
    <?php

} ?>    
                </div>
    
asked by anonymous 28.10.2017 / 17:10

1 answer

1

Since the if statement that checks for $_POST['aceitar'] exists within the while loop, regardless of the chosen deal, it runs for all rows in the database. One way to avoid this is to get the ifs that check for $_POST['aceitar'] and $_POST['rejeitar'] from within the while loop and place a hidden input inside the form along with the accept and reject buttons (serves to send the chosen option id ). Here's how:

<div class="container2">
    <div class="col-sm-10">
        <div class="row">
            <div class="titulo_1">
            <h1>Ofertas recebidas</h1>
            </br>
                </div> 

                    <?php 
                    $id_usuario = $_SESSION["id"];
                    $anuncio = $_GET["id"];

                    $sql = "SELECT * FROM ofertas = OF 
                    INNER JOIN anuncios = AN
                    ON OF.id_anuncio = AN.ID
                    WHERE AN.id_usuario = '$id_usuario' AND OF.status = 'ANALISE'";

                    while($sql = mysql_fetch_array($query2))
                    { ?>

                        <div class="col-sm-3">
                        </br>
                        <div class="text-center"><strong>ID do usuário que enviou a oferta: </strong><?php echo $sql['id_usuario_oferta']; ?></div>
                        <div class="text-center"><strong>ID da oferta: </strong><?php echo $sql['ID_OF']; ?></div>
                        <div class="text-center"><strong>ID do anúncio: </strong><?php echo $sql['ID']; ?></div>
                        <div class="text-center"><strong>Usuário que enviou a oferta: </strong><?php echo $sql['nomeusuario_oferta']; ?></div>
                        <div class="text-center"><strong>Produto: </strong><?php echo $sql['produto']; ?></div>
                        <div class="text-center"><strong>Valor oferta: </strong><?php echo $sql['valoroferta']; ?></div>
                        <div class="text-center"><strong>Valor de recompensa que você informou: </strong><?php echo $sql['valorrecompensa']; ?></div>
                        <div class="text-center"><strong>Viagem que ele irá fazer: </strong><?php echo $sql['viagemcompra']; ?></div>
                        <div class="text-center"><strong>Comentário: </strong><?php echo $sql['comentario']; ?></div>
                        <div class="text-center"><strong>Oferta enviada em: </strong><?php echo $sql['dataoferta']; ?></div>
                        </br>

                        <form method="post" action="">
                            <!-- Adicione um campo oculto para armazena o id da oferta escolhida -->
                            <input type="hidden" name="ID_OF" value="<?php echo $sql['ID_OF']; ?>">
                            <button type="submit" name="aceitar" id="aceitar" value="NEGOCIACAO"class="btn btn-success btn-flat">Aceitar</button> 
                            <button type="submit" name="rejeitar" id="rejeitar" value="REJEITADO" class="btn btn-danger btn-flat">Rejeitar</button>
                        </form>
            </div>
    <?php
    } 
    ?>    
</div>

<?php
/******************************************************************************************
Verifique a submissão do formulario fora do laço while
******************************************************************************************/
if(isset($_POST['ID_OF'])){
    $IDoferta = $_POST['ID_OF'];
    if(isset($_POST['aceitar'])) {
        $query = "UPDATE ofertas SET status = 'NEGOCIACAO' WHERE ID_OF = '$IDoferta'";
            mysql_query($query);
            echo "<meta http-equiv='refresh' content='0, url=index.php'";
    }      
    if(isset($_POST['rejeitar'])) {
        $query = "UPDATE ofertas SET status = 'REJEITADO' WHERE ID_OF = '$IDoferta' LIMIT 1";
        mysql_query($query);
        echo "<meta http-equiv='refresh' content='0, url=index.php'";
    }
}
?>
    
28.10.2017 / 18:00