What is missing from the loop to update in all the records that are traversed?

1
if( isset($_POST['asistotales']) && !empty($_POST['numsesiones']) )  {

   $asistotales  = $_POST['asistotales']; //valores de arreglo recibido
   $numsesiones  = $_POST['numsesiones'];  //valor recibido de un input


  $n4 = count($asistotales);  //cuenta elementos del array

  $num4_alumno = 1;  //contador de registros de alumno en gpo
  for ($i=0; $i < $n4; $i += 1, ++$num4_alumno) {
    //regla de 3
    $a = $asistotales[$i];
    $regla = $a * 100;
    $asis = $regla/$numsesiones;
             //no funca
             $gpo  = $grupo[$num4_alumno-1];
             $al   = $alumno[$num4_alumno-1];     

            $sql = mysqli_query($connect, "UPDATE alumno_grupo SET asistencias = '".$a."'
           WHERE idAlumno = $al AND idGrupo = $gpo") or die (mysqli_error($connect));
    
asked by Armando Arellano 04.11.2016 в 07:12
source

2 answers

0

I do not know what error you have, but I would do it differently:

  • In for replace $i += 1 with i++ which is the usual way of doing it and it seems clearer.
  • Do you make ++$num4_alumno below and then use the variable by removing 1? If you only use this variable to get the values of $grupo and $alumno , make the increment at the end of for , if you have any other function before finishing the for , just after obtaining those values.
  • Then to prepare the statement, if you concatenate the variables in a String between signs {} PHP recognizes that it is a variable and is faster, for example:

    "UPDATE student_group SET assists = '{$ a}'            WHERE idAlumn = {$ al} AND idGroup = {$ gpo} "

  • In any case I would compose the queries, saving them in an array and execute them later. You can do a function that only performs the execution of a group of queries, so you can put them all in a transaction or not, as the case, but above all you can print them all at a given time and see if they are correct syntactically or if they receive all the expected values.

        
    answered by 04.11.2016 / 09:30
    source
    0

    In your loop, for changes ++$num4_alumno by $num4_alumno++ . The first one makes you a pre-increment so the variable is added for the iteration that is taking place at that moment. For the second case, one adds to variable $num4_alumno but for the next iteration.

        
    answered by 04.11.2016 в 17:11