Problems updating PHP MYSQL data series

0

Good, I have a modal where they load me the qualifications of a student, if I want to update those qualifications, only I update the last data. Attached modal image

Also the code that I have to update on the modal page is:

if(!empty($_POST['nota_act'])){
                    $id=limpiar($_POST['id2']);                     
                    $num = count($id);
                    for($i=0;$i<$num;$i++)
                    {
                    $nota_act=limpiar($_POST['nota_act']);
                    $num1 = count($nota_act);

                print_r($id[$i].'<BR>');
                        for($z=0;$z<$num1;$z++)
                        {
                            print_r($nota_act[$z]);
                            $oActualizar=new Proceso_Calificar($id[$i],'','','',$nota_act[$z],'','');
                            $oActualizar->actualizar();
                        }
                    }
                        echo mensajes('Nota Actualizada con Exito','verde');

                }

Modal code is as follows:

                        <div id="a<?php echo $row['periodo'].$row['materia']; ?>" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                        <form name="form5" method="post" action="">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                            <h3 id="myModalLabel" align="center">
                                Modificar Notas <br>Materia "<?php echo $oMateria->consultar('nombre'); ?>"
                            </h3>
                        </div>
                        <div class="modal-body">
                            <table class="table table-bordered table table-hover">
                              <tr class="well">
                                <td><strong><center>Actividad</center></strong></td>
                                <td><strong><center>Nota</center></strong></td>
                              </tr>
                              <?php 
                                $paa=mysqli_query($conexion,"SELECT * FROM notas 
                                WHERE alumno='".$row['alumno']."' and periodo='".$row['periodo']."' and materia='".$row['materia']."'");                    
                                while($dato=mysqli_fetch_array($paa)){
                                    $oAct=new Consultar_Actividad($dato['actividad']);
                              ?>
                              <tr>
                                <td><input type="hidden" name="id2[]" value="<?php echo $dato['id']; ?>"><center><?php echo $oAct->consultar('nombre'); ?></center></td>
                                <td>
                                <input type="number" min="<?php echo $minima_nota; ?>" max="<?php echo $maxima_nota; ?>" value="<?php echo $dato['valor']; ?>" name="nota_act[]" autocomplete="off" size="4">
                                </td>
                              </tr>
                              <?php } ?>
                            </table>
                        </div>
                        <div class="modal-footer">
                            <button class="btn" data-dismiss="modal" aria-hidden="true"><strong>Cerrar</strong></button>
                            <button type="submit" class="btn btn-primary"><strong><i class="icon-refresh"></i> Actualizar</strong></button>
                        </div>
                        </form>

And the class in the Classes.php file is:

class Proceso_Calificar extends Conexiones{
var $id;        var $materia;      var $alumno;         var $actividad;     var $valor;
var $periodo;   var $fecha;

function __construct($id, $materia, $alumno, $actividad, $valor, $periodo, $fecha){
    $this->id=$id;              $this->materia=$materia;    
    $this->alumno=$alumno;      $this->actividad=$actividad;  
    $this->valor=$valor;        $this->periodo=$periodo;            $this->fecha=$fecha;
}

function guardar(){
    $id=$this->id;              $materia=$this->materia;    
    $alumno=$this->alumno;      $actividad=$this->actividad;        
    $valor=$this->valor;        $periodo=$this->periodo;            $fecha=$this->fecha;
    parent::__construct();
    $sql = "INSERT INTO notas (materia, alumno, actividad, valor, periodo, fecha) 
                              VALUES ('$materia','$alumno','$actividad','$valor','$periodo','$fecha')";
    $query = $this->conexion_db->query($sql);

}

function actualizar(){
    $id=$this->id;              $materia=$this->materia;    
    $alumno=$this->alumno;      $actividad=$this->actividad;        
    $valor=$this->valor;        $periodo=$this->periodo;            $fecha=$this->fecha;
    parent::__construct();
    $sql = "UPDATE notas SET valor='$valor' WHERE id='$id'";
    $query = $this->conexion_db->query($sql);

}

}

But only the last data keeps me in this case, unit 4 and the others do not.

    
asked by Francisco 14.05.2017 в 03:40
source

1 answer

0

The two for loops for inserting are poorly stated.

If we see the process for unit 1, insert the process from the first to the last one that will be left. The same for the two, the three, ...

It should be a single loop and have the element and note ids aligned.

Here is a help with the use of arrays in link

You can use for example a single array of notes and put in your index the id of the unit.

Example:

Nota_act[6]  

Then you go through a for key type = > value and insert each value to the key that corresponds to it.

I add a very simple example based on your code.

For the page the loop of the notes would be:

<?php 
    $paa=mysqli_query($conexion,"SELECT * FROM notas WHERE alumno='".$row['alumno']."' and periodo='".$row['periodo']."' and materia='".$row['materia']."'");                    
    while($dato = mysqli_fetch_array($paa)){
        $oAct=new Consultar_Actividad($dato['actividad']);
        $name   = $oAct->consultar('nombre');
        $id     = $dato['id'];
    ?>
    <tr>
        <td><center><?php echo $name ?></center></td>
        <td>
            <input type="number" min="<?php echo $minima_nota; ?>" max="<?php echo $maxima_nota; ?>" 
            value="<?php echo $dato['valor']; ?>" name="nota_act[<?php echo $id ?>]" autocomplete="off" size="4">
        </td>
    </tr>
<?php } ?>

For the backend that records the notes, it would be:

if(!empty($_POST['nota_act'])){
    foreach ($_POST['nota_act'] as $clave => $valor) {
        $id = limpiar($clave); 
        $nota = limpiar($valor);
        if(!empty($nota) || $nota == "0" ){ // empty evalua "0" como vacío
            $oActualizar=new Proceso_Calificar($id,'','','',$nota,'','');
            $oActualizar->actualizar();
        }
    }
    echo mensajes('Nota Actualizada con Éxito','verde');
}
    
answered by 14.05.2017 / 16:15
source