Montart loop with variables for PDO exclusion

Asked

Viewed 44 times

1

After a filter I have this form with the users and their services, what I am trying to do is send the information to delete the records with the parameters set in the form, in case it would be the User ID and the Service, then I will insert the ones with checked marked, that is, can be multiple records at the same time being sent.

This is my form:

inserir a descrição da imagem aqui

My variables are like this in my form:

 <input type="checkbox" name="Check[]"  class="checkbox1" <?php if ($retorno >= 1) { echo "checked='checked'"; } ?> value="<?php echo $IdUsuario;  ?>">
 <input name="IdUsuario" type="hidden" value="<?php echo $IdUsuario;  ?>" />  
 <input name="IdServico" type="hidden" value="<?php echo $IdServico;  ?>" />              
 <input name="IdInterface" type="hidden" value="<?php echo $IdInterface;  ?>" /

I need to run one loop to an effect an exclusion of my BD, but I have no way to make this loop, because I only get the variables from a form, there is some way to do it only with those variables?

The variables I get are these:

$IdUsuario = (isset($_POST['IdUsuario'])) ? $_POST['IdUsuario'] : '';
$IdServico = (isset($_POST['IdServico'])) ? $_POST['IdServico'] : '';

My exclusion code is like this:

$sql = "DELETE FROM `gasUsuarioServico` WHERE `IdUsuario` = :IdUsuario AND `IdServico` = :IdServico";
$stm = $conexao->prepare($sql);
$stm->bindValue(':IdUsuario', $IdUsuario, ':IdServico', $IdServico);
$retorno = $stm->execute();
  • I don’t understand, can you offer more details? Why can’t you use these variables in the loop?

  • Hello @Magichat, this is exactly my difficulty, how to build a loop with variables.

  • Let me try to understand the situation... From what I realize the data contained in the variables are unique(because they are ids), the loop performs an action repeatedly until a condition is satisfied, however in this case(apparently) you will not be able to perform the action more than once... So then I’m not understanding the need for the loop... Try to explain in more detail the operation of this process you want, e.g., where the values of these ids will come from(although you mentioned a form)... I mean, will be sent 1 user and service at a time?

1 answer

2


Well I made the code based on your.

I left it documented in the code.

<?php

if(isset($_POST['check'])){ //Verifica se tem algum usuário selecionado

   $checked = $_POST['check'];//Posicoes selecionadas

   $IdUsuario = $_POST['IdUsuario']; //Array de usuarios
   $IdServico = $_POST['IdServico']; //Array de Servicos
   $IdInterface = $_POST['IdInterface']; //Array de Interfaces

   $total = count($checked); //Quantidade de usuários selecionados

    for($i=0;$i<$total;$i++){ //Varrer eles

        //echo $IdUsuario[$checked[$i]]; //Usuario selecionado
        //echo $IdServico[$checked[$i]]; //Servico selecionado
        //echo $IdInterface[$checked[$i]]; //Interface selecionado

        $sql = "DELETE FROM `gasUsuarioServico` WHERE `IdUsuario` = :IdUsuario AND `IdServico` = :IdServico";
        $stm = $conexao->prepare($sql);
        $stm->bindValue(':IdUsuario', $IdUsuario[$checked[$i]], ':IdServico', $IdServico[$checked[$i]]);
        $retorno = $stm->execute();        
    }    
}
?>
<form name="form1" method="post"> <!-- Formulário -->
<?php 
    //Dados colocados fixos, mas deve alterar para a sua busca
    $db_usuarios = array( 
                    array("user"=>"Usuário 1",
                          "usuario"=>"1",
                          "servico"=>"1",
                          "interface"=>"1"),
                    array("user"=>"Usuário 2",
                          "usuario"=>"2",
                          "servico"=>"2",
                          "interface"=>"2"),
                   array("user"=>"Usuário 3",
                          "usuario"=>"3",
                          "servico"=>"3",
                          "interface"=>"3")
                );

   $total_usuarios = count($db_usuarios); //Alterar para a sua busca

for($i=0; $i<$total_usuarios; $i++){ //Criar os usuários dinamicamente
?>

<input type="checkbox" name="check[]"  class="checkbox1" value="<?php echo($i); ?>"><?php echo($db_usuarios[$i]['user']); ?>
<input name="IdUsuario[]" type="hidden" value="<?php echo($db_usuarios[$i]['usuario']); ?>" /> 
<input name="IdServico[]" type="hidden" value="<?php echo($db_usuarios[$i]['servico']); ?>" />   
<input name="IdInterface[]" type="hidden" value="<?php echo($db_usuarios[$i]['interface']); ?>"/>

<?php

}

?>

<input type="submit" value="Enviar"> <!-- Enviar -->

</form>
  • Thanks for the great help @Everson, very interesting this solution.

Browser other questions tagged

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