Error in query between PHP and MYSQL

0

Good evening.

I do not know why the query does not run.

<html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
</html>
<?php
include 'conexion.php';
$grupo= $_POST['grupo'];

    //Sentencia y ejecución de la misma
//  $idns=count($_POST['idn']);
$i="1";
    if(isset($nota[$i])){
        for ($i = 1; $i < count($_POST['idn']); $i++) {
      $sentencia23=mysqli_query($conexion,"SELECT * FROM calificaciones WHERE grupo='$grupo'");
            $numero = mysqli_num_rows($sentencia23);
      $ii="1"; $nota="nota"; $idnota="idnota";
      While($resultado23=mysqli_fetch_array($sentencia23)){
      $nota.''.$ii = $_POST[$nota.''.$ii][$i];
            $idnota.''.$ii = $_POST[$idnota.''.$ii][$i];

 $result = $conexion->query("UPDATE notas SET nota = \'$nota.''.$ii\' WHERE Idn = \''$idnota.''.$ii'\'")

$ii++; }
}

    //$promedio=$_POST['resultado'][$i];

}

    //Comprobación de la actualización
    if ($resultado = $conexion->query($consulta)) {
        echo "Los datos han sido actualizados satisfactoriamente.";
    } else {
        echo "Intente nuevamente, no se ha podido ejecutar la actualización.".$ejecucion->error;
    }
    mysqli_close($conexion);
?>
    
asked by Fredy Aristizábal Aristizábal 14.11.2017 в 17:16
source

1 answer

0

In this loop (which is also missing a semicolon at the end of the UPDATE statement);

 $ii="1"; $nota="nota"; $idnota="idnota";

 while($resultado23=mysqli_fetch_array($sentencia23)){
      $nota.''.$ii = $_POST[$nota.''.$ii][$i];
      $idnota.''.$ii = $_POST[$idnota.''.$ii][$i];

      $result = $conexion->query("UPDATE notas SET nota = \'$nota.''.$ii\' WHERE Idn = \''$idnota.''.$ii'\'");
      $ii++; 
}

You are assigning values not to a variable but to a string:

     'nota1' = $_POST[$nota.''.$ii][$i];

What you would want to do is generate a variable called $nota1

      // Defines la variable $nota1
      ${$nota.''.$ii} = $_POST[$nota.''.$ii][$i];
      // Defines la variable $idnota1
      ${$idnota.''.$ii} = $_POST[$idnota.''.$ii][$i];

Now I wonder, if the only purpose of your loop is to update the notes, why not simply redefine the value of $nota and $idnota in each iteration?

      $nota = $_POST[$nota.''.$ii][$i];
      $idnota = $_POST[$idnota.''.$ii][$i];    

Finally, it is not a good idea to manually interpolate variables in your queries. You should use a prepared statement and then make a bind of the parameters:

$stmt = $mysqli->prepare("UPDATE notas SET nota = ? WHERE Idn = ?");

while($resultado23=mysqli_fetch_array($sentencia23)){
    $stmt->bind_param('ss', $_POST[$nota.''.$ii][$i], $_POST[$idnota.''.$ii][$i]);
    $stmp->execute();
}

You prepare the sentence once and then reuse it. Or better yet, validating parameters:

$stmt = $mysqli->prepare("UPDATE notas SET nota = ? WHERE Idn = ?");
while($resultado23=mysqli_fetch_array($sentencia23)){
    if(isset($_POST[$nota.''.$ii]) && isset($_POST[$nota.''.$ii][$i]) && isset($_POST[$idnota.''.$ii]) && isset($_POST[$idnota.''.$ii][$i]) {
        $stmt->bind_param('ss', $_POST[$nota.''.$ii][$i], $_POST[$idnota.''.$ii][$i]);
        $stmt->execute();
    }
}
    
answered by 14.11.2017 в 18:11