Obtain selected values of the checkbox array, and of the arrangement of inputs through loops. To execute a query with the obtained values

0

In this code I try to get values of checkbox selected, within a loop, and in another nested loop you get values of inputs fixed. With these iterated values obtained within the loops, I wanted to execute a delete , setting as parameters where the values of each alumno selected and the grupos that are values of predefined inputs. I tried to execute the query with the values of n checkbox that have been selected and with their respective groups, but it is not enough to execute it for all the found records, it seems to me it is a logic error in the loops.

The problem is that the statement is not executed for all selected checkbox values. For example, if 40 were selected, the delete is only executed for 34,

 $alumno     = $_POST["idalumno"]; //arreglo [] de alumnos (recogiendo id de arreglo de checkbox)
  $idgrupo    = $_POST["idgrupo"]; //arreglo [] de grupos, inputs (hidden)

  for ($i=0; $i < count($alumno); $i++) { //mientras la variable sea menor al tamaño de arreglo va aumentando de uno en uno

       $alum = $alumno[$i]; //obteniendo cada idalumno

       for ($a=0; $a < count($idgrupo); $a++) {

             $gpo = $idgrupo[$a]; //obteniendo id de cada grupo

       $verifica = "SELECT * FROM alumno_grupo WHERE idAlumno = '{$alum}' and idGrupo = '{$gpo}'";
       $consul = mysqli_query($conexion, $verifica)or die (mysqli_error($conexion)); 
       $num_rows = mysqli_num_rows($consul);
       if ($num_rows > 0) {

       $cons = "DELETE FROM alumno_grupo WHERE idAlumno = '{$alum}' AND idGrupo = '{$gpo}'";  

       $consulta = mysqli_query($conexion, $cons)or die (mysqli_error($conexion));  

       if ($consulta){
  ?>
   <script>
   alert('ALUMNO DADO DE BAJA DE LOS GRUPOS-MATERIA, CORRECTAMENTE')
   location.href = "../View/Admin/baja_alum.php"; 
   </script>

  <?php
  } else {  //si no se ejecuta la consulta
  ?>
   <script>
   alert('ATENCION !!!! ERROR AL ELIMINAR')
   location.href = "../View/Admin/baja_alum.php"; 
   </script>

  <?php
  }
       } else {  //si no se encuentran los registros
       ?>

      <script>
       alert('ATENCIÓN!!: NO SE ENCONTRO REGISTRO CON ESE ALUMNO Y GRUPO-MATERIA')
       location.href = "../View/Admin/baja_alum.php"; 
      </script>

       <?php
       } //else
    } //for
  } //for
    
asked by Armando Arellano 02.01.2017 в 08:53
source

3 answers

1

better with a preparation for the deletion and a query for the query

   $conexion = new mysqli("localhost", "my_user", "my_password", "world");
   for ($i=0; $i < count($alumno); $i++) { //mientras la variable sea menor al tamaño de arreglo va aumentando de uno en uno

       $alum = $alumno[$i]; //obteniendo cada idalumno

       for ($a=0; $a < count($idgrupo); $a++) {

          $gpo = $idgrupo[$a]; //obteniendo id de cada grupo

          $resultado = $conexion->query("SELECT * FROM alumno_grupo WHERE idAlumno = '{$alum}' and idGrupo = '{$gpo}'");
          if ( $resultado->num_rows > 0){

              $sth = $conexion->prepare("DELETE FROM alumno_grupo WHERE idAlumno = ? AND idGrupo = ?);  
              $sth->bind_param("ii", $alum, $gpo); //i para integer , s para string
              if ($sth->execute()){
                 //codigo script
              }else{
              //codigo script

             }
         } else {
    }
    
answered by 03.01.2017 в 23:14
1

It can be a problem of both the design and the loop. I suggest that before anything you check that you have the same keys in the two arrays, if so, then use the following:

It is probably a good idea to escape all groups and students if they are string, you can use something like this:

array_walk($idgrupo, function(&$value, &$key) {
  $value = mysqli_real_escape_string($conexion, $value);
});

Then we can do the loop:

$alumno     = $_POST["idalumno"]; //por si falta...
$idgrupo    = $_POST["idgrupo"];
$array_final = [];
foreach($alumno AS $id => $nombre){      
   $array_final[] = (mysqli_query($conexion, "DELETE FROM alumno_grupo WHERE idAlumno = '{$id}' AND idGrupo IN (".implode(',', $idgrupo).")") ? "El alumno $nombre Se ha borrado de todos los grupos seleccionados." : "Hay un error");
}

The Javascript below will print what we send ... it is not necessary to send 5000 alerts + 5000 <script>

echo '<script>
 alert('.implode("\n", $array_final).');
 location.href = "../View/Admin/baja_alum.php"; 
</script>';
    
answered by 04.01.2017 в 09:48
1

You can try this in your javascript:

var seleccionados = $('input:checkbox:checked').map(function() {
            return this.value;
        }).get();
        $.ajax({
            type: "POST",
            url: 'accione.php',
            data: { arreglo : seleccionados },
            // seleecionados= [id,id,id,id,id...]

        });

and your php:

 $data = $_POST["arreglo"];
   foreach($data as $alumno){
                $sql = "delete from tabla where group_id ='".$alumno."'";
    mysqli_query($cx,$sql);
        }
    
answered by 10.02.2017 в 03:24