Doing action on all data

Asked

Viewed 42 times

1

I am developing an ad system, where it is possible to send offers to the ad owner and then carry out the approval or rejection of the offer, if you approve and then negotiate with those who offered.

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

I did a search here for the forum, but I didn’t find anything like it.

Follow the code (Obs: when I played 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>
  • This error usually happens when there is an error in the clause where sql, which instead of making the change on a database line, by id for example, does it to all rows. I think the error is after Where. Exchange the '$IDoferta' for " + $IDoferta + "

  • Opa William, thank you very much friend! I managed to resolve with the answer below.

1 answer

1

Like the if that checks if it exists $_POST['aceitar'] is within the while loop, regardless of the chosen offer it runs for all bank lines. One way to avoid this is to take out the ifs that check by $_POST['aceitar'] and $_POST['rejeitar'] from inside the while loop and place an input of type Hidden inside the form along with the accept and reject buttons (serves to send the id of the chosen option). See how it looks:

<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'";
    }
}
?>
  • Man, that was it!!! I tested it here and it was perfect! Thank you very much my friend!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.