PHP mysqli_query () It does not work

0

I am learning to connect MySQLi PHP with MySQL, I made this CRUD but it does not work correctly. Only works in table Alumno , but not in table Matricula .

I already tried with mysqli_multi_query() and with different ways to make it work and I do not find how.

include 'Conexion.php';
if(isset($_POST['Registrar']))
{ 
$IDalumn = "";
$nomalumn ="";
$apealumn ="";
$IDalumn = $_POST['IdAlu'];
$nomalumn = $_POST['NomA'];
$apealumn = $_POST['ApeA'];
$IDgrado = $_POST["IDgrado"];
$fechaingre = $_POST["FechaIngre"];


$query1 = "INSERT INTO matricula(IDalumno,IDgrado,FechaIngreso)VALUES 
('$IDalumn','$IDgrado','$fechaingre')";
$query2 = "INSERT INTO alumno(IDalumno,Nombres,Apellidos)VALUES 
('$IDalumn','$nomalumn','$apealumn')";
$Create = mysqli_query($conexion,$query1);
$Create1 = mysqli_query($conexion,$query2);

mysqli_close($conexion);

}

Here is the HTML code

<table>  
    <ul> 
      <li>
          <input name="IdAlu" id="IdAlu" type="text" maxlength="5"> 
Digite la identificacion del alumno: 
      </li>
      <li>
          <input name="NomA" id="NomA" type="text" maxlength="40"> 
Digite Los nombres del alumno:
      </li>
      <li>
          <input name="ApeA" id="ApeA" type="text" maxlength="40"> 
Digite Los apellidos del alumno:
      </li>
      <li>
          <input name="IDgrado" id="IDgrado" type="text" 
maxlength="2" > Digite la identificacion de grado al que ingresa el 
alumno:
      </li>
      <li>
          <input name="FechaIngre" id="FechaIngre" type="date"> 
Digite la fecha en la que ingresa el alumno:
      </li>
        <input type="submit" name="Registrar" value="AGREGAR"/>
        <input type="submit" name="Actualizar" Value="ACTUALIZAR">


    </ul>    
  </table> 
    
asked by Gammath.rar 25.11.2017 в 01:20
source

1 answer

1

This is my proposal. Which includes the following:

  • Use of queries prepared to give security to the code. Not using this type of query in data that comes from external sources to the same query can seriously endanger not only the database, but the entire system.
  • Use of object-oriented programming instead of procedural programming. It is clearer and more adapted to modern times. If you want to use procedural programming, the code can be adapted.
  • A controlled code is written, using a variable that gathers all its eventualities in the code. At the end, the message contained in that variable will be printed. For me, that's a way of not writing mute code. Here, the code is obliged to say what is happening to it at all times. That way, the programmer controls the code, and not the other way around.
  • If you have any questions, you can ask in comments.

    Here goes the code, I hope it serves you:

    <?php
    
    include 'Conexion.php';
    if(isset($_POST['Registrar']))
    {
        if ($conexion){
            $IDalumn = $_POST['IdAlu'];
            $nomalumn = $_POST['NomA'];
            $apealumn = $_POST['ApeA'];
            $IDgrado = $_POST['IDgrado'];
            $fechaingre = $_POST['FechaIngre'];
            $arrResultado=array();
    
            $matriculaSQL = 'INSERT INTO matricula (IDalumno, IDgrado, FechaIngreso) VALUES (?,?,?)';
            $stmt=$conexion->prepare($matriculaSQL);
            if ($stmt){
                $stmt->bind_param('sss', $IDalumn, $IDgrado, $fechaingre);
                $crearMatricula=$stmt->execute();
    
                if($crearMatricula){
                    $arrResultado['mensaje']='¡Matrícula creada! Se agregaron '.$stmt->affected_rows.' a la tabla.';
                }else{
                    $arrResultado['mensaje']='Error creando la Matricula: '.$stmt->error;
                }
                $stmt->close();
    
            }else{
                $arrResultado['mensaje']='Error en la consulta SQL de Matricula: '.$conexion->error;
            }
    
    
            $alumnoSQL = 'INSERT INTO alumno (IDalumno, Nombres, Apellidos) VALUES (?,?,?)';
            $stmt=$conexion->prepare($alumnoSQL);
            if ($stmt){
                $stmt->bind_param("sss", $IDalumn, $nomalumn, $apealumn);
                $crearAlumno=$stmt->execute();
    
                if($crearAlumno){
                    $arrResultado['mensaje'] .= '<br />¡Alumno creado! Se agregaron '.$stmt->affected_rows.' a la tabla.';
                }else{
                    $arrResultado['mensaje']='<br />Error creando el Alumno: '.$stmt->error;
                }
                $stmt->close();
    
    
            }else{
                $arrResultado['mensaje']='<br />Error en la consulta SQL de Alumno: '.$conexion->error;
            }
    
        }else{
            $arrResultado['mensaje']='Error en la conexión a la base de datos';
        }
    
    }else{
        $arrResultado['mensaje']='No hay datos en Registrar';
    }
    
    echo $arrResultado['mensaje'];
    
    ?>
    
        
    answered by 25.11.2017 в 02:34