Montart loop with variables to exclude with PDO

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, enter the ones that are checked with check, that is, several records can be sent at the same time.

This is my form:

Myvariablesarelikethisinmyform:

<inputtype="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 execute a loop for an exclude from my BD , but I can not do this loop, since I only get the variables coming from a form, is there any way to do it with only these variables ?

The variables I get are these:

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

My opt-out code looks 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();
    
asked by anonymous 26.05.2017 / 14:07

1 answer

2

Well I made the code based on yours.

I've 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>
    
26.05.2017 / 19:26