CHANGE PHP STATUS

Asked

Viewed 671 times

0

I’m having a problem changing the status of users of my database , when I will change the status of some user it does not approve the user of that line and yes last user what can be?

php log.

  <?php  
                //Consulta
                $buscarusuario=$pdo->prepare("SELECT * FROM usuario");
                $buscarusuario->execute();

                //atribuindo dados á variavel
                $linha = $buscarusuario->fetchAll(PDO::FETCH_ASSOC);

                //percorrendo a variavel para listar os dados
                foreach ($linha as $listar) {
                    $iduser = $listar['id'];
                    echo "<tr>";
                    echo " <td>".$listar['id']."</td>";
                    echo "<td>".$listar['nome']."</td>";
                    if($listar['status'] > 0 ){
                    echo "<td class='success text-success'>Aprovado 
  <form method='post' action='pg/mudastatus.php'>
    <input type='hidden' name='desaprovauser' value='$iduser'>
      <button type='submit' class='btn btn-xs btn-success alinha-btn' name='desaprova' value='desaprovar'>Desaprovar</button>

                    </td>";
                  }else{
                    echo "<td class='danger text-danger'> Aguardando aprovação 
  <form method='post' action='pg/mudastatus.php'>
   <input type='hidden' name='aprovauser' value='$iduser'>
   <button type='submit' class='btn btn-xs btn-danger alinha-btn' name='aprova' value='aprovar' >Aprovar</button>
</form>

                    </td>";
                  } 
  }
              ?>

mudastatus.php

    if(isset($_POST['aprova'])){

   $atualizarstatus = $pdo->prepare("UPDATE usuario SET status=1 WHERE id='".$_POST["aprovauser"]."' ");
   $atualizarstatus->execute();
   $linha = $atualizarstatus->rowCount();

   if($linha > 0){
     header("location:../logado.php");
   }else{
    echo "Erro ao Mudar status";
   }
}elseif (isset($_POST['desaprova'])){

   $atualizarstatus = $pdo->prepare("UPDATE usuario SET status=0 WHERE id='".$_POST["desaprovauser"]."' ");
   $atualizarstatus->execute();
   $linha = $atualizarstatus->rowCount();

   if($linha > 0){
     header("location:../logado.php");
   }else{
    echo "Erro ao Mudar status";
    header("location:../logado.php");
   }
}
  • 1

    We’re missing a </form> in the first block, it is good to correct this before proceeding with the tests. Then you can dry this code well by making a form just for everything and taking the Hidden, but before you can make it work the way you know, not to complicate too much. Then, just take out the Hidden fields, and use the user ID in Buttons value approves and disapproves.

  • I am voting to close, because it is a typo, and the answers do not solve the problem of the code, besides containing incorrect statements, disturbing other users. See proof of operation with names repeated here: http://codepen.io/bacco/pen/BzkBPy

2 answers

0


First a brief comment on security, since it is using PDO and prepare... it is recommended that you pass the variables directly in the query...

$atualizarstatus = $pdo->prepare("UPDATE usuario SET status=1 WHERE id=? ");
$atualizarstatus->execute(array($_POST["aprovauser"]));

Thus, you would already avoid the injection of sql, through direct variables inserted in the query. Now to return to your question perhaps it is that, as I understand it, your code has a user and the button approve or disapprove. And if they have more than one, would be: User 1 Approve User 2 User 3 Being this approve the Ubmit button, Cvoce agrees that when I click on approve I will send normally, but as there are more than one approved button, IE with the same name, not time to pick up the value it will not know which Voce clicked and rather catch the last boot, That’s what’s going on. I hope I’ve helped

  • the buttons are in separate Forms (they should actually be. it looks like a </form is missing>).

  • It is no use to be in different shapes if the name property is equal

  • You can have as many repeat names as you want in different Rms. Do not confuse with ID. ID is one per HTML, name vc can repeat in different Rms. Or even in the same, as long as the backend knows what to do. Inclusive, PHP array uses the same name in several fields.

  • I’ve already tested I’m not as layman as you think

  • I don’t remember commenting on anyone being or not being a layman, I just pointed out the mistake in the information so that you had opportunity to correct. Just that. And the codepen is just to facilitate the test for you and anyone else who has doubts. http://codepen.io/bacco/pen/Rgbrd

-2

You’re creating multiple Hidden input with the same name, so you’re not sure what value comes in the backend, try changing your logic so each input has a unique name.

Browser other questions tagged

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