How to show the records that I am uploading in the database in the table html?

1

This is the code in php and html that I have works well, saves the records that I charge, but what it does not do is show the row that I just loaded on the page after I click on the "load" button I have to reload the page and it gives me the warning of the empty fields. I want you to click on the row I just entered after clicking.

     <?php
     include("conexion.php");

     $enlace=conectarse();
     $query2="SELECT * FROM netbook_alumno" ;//consulta para mostrar la db
     $resultado = $enlace->query($query2);
     mysqli_close($enlace);

  ///////////////////////////////////////////////////////


    if(isset($_POST["cargar"])){
       $error=array();
    if(empty($_POST['campo1'])){
       $error[]='Por favor no dejes el campo de numero de CUIL en blanco';
   }else{
       $campo1=$_POST['campo1'];
   }
   if(empty($_POST['campo2'])){
      $error[]="No deje el campo de nombre en blanco";
   }else{
      $campo2=$_POST['campo2'];
   }
  if(empty($_POST['campo3'])){
      $error[]="No deje el campo de ID-NetBook en blanco";
  }else{
     $campo3=$_POST['campo3']; 
  }
     $campo4=$_POST['campo4'];

    if(empty($error)){
       $conexion=conectarse();
     //pregunta si hay registros,si no hay hace el insert
      $consultando_query="SELECT * FROM netbook_alumno WHERE cuil = 
      '$campo1'";
      $respuesta_query = $conexion->query($consultando_query);
      $contar=mysqli_num_rows($respuesta_query);
    //hace el insert
    if($contar==0){
        $conexion2=conectarse();
        $insertar_datos="INSERT INTO netbook_alumno 
     (cuil,nombre,id_netbook_alumno,observaciones)VALUES
        ('$campo1','$campo2','$campo3','$campo4')";
        $consulta_query2=$conexion2->query($insertar_datos);
        echo"
        <script>alert('Datos Guardados');</script>";
          }else{
         /*ventana pop up*/
         print '<script>'; 
         print 'alert("Esa netbook ya existe.");'; 
         print '</script>';  
          }
      }else{
        foreach($error as  $values){
        echo '<li>'.$values.'</li>';

         }
      }
   }
   <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">

<title>Asignar NetBook a Alumno</title>
</head>
<body >

<h1>Cambios&nbsp; Stock&nbsp; Netbooks</h1>
 <form action="asignarAAlumno.php" name="mi_formulario" method="post">
 <p>
  <label for="textfield">CUIL:</label>
  <input type="text" name="campo1" id="campo1"  value= "<?php echo  $c1 ?>">
</p> 
<p>
 <label for="texfield">Nombre:</label>
 <input type="text" name="campo2" id="campo2" value = "<?php echo $c2 ?>">
</p>
<p>
  <label for="textfield">ID-NetBook:</label>
  <input type="text" name="campo3" id="campo3" value ="<?php echo $c3 ?>">
 </p>
 <p>
  <label for="textfield">Observaciones</label>
  <input type="text" name="campo4" id="campo4" value="<?php echo $c4 ?>">
 </p>
<p>
<input type="submit" name="cargar" id="button" value="Cargar">
</p>
</form>
<br>

<table border="3" >
    <tr  id="encabezado">
        <td>Cuil</td>
        <td>Nombre</td>
        <td>ID-NetBook</td>
        <td>Observaciones</td>

    </tr>

    <?php 

   while($campo=mysqli_fetch_row($resultado)){

     ?>
    <tr>
        <td><?php echo $campo[0] ?></td>
        <td><?php echo $campo[1] ?></td>
        <td><?php echo $campo[2] ?></td>
        <td><?php echo $campo[3] ?></td>
    </tr>
   <?php 
     }
   ?>
      </table>
    </br>
  </body>
</html>
    
asked by Masterweed 10.07.2018 в 17:57
source

1 answer

1

What you need is to make your query after entering the new data. Also, you do not need to create a connection every time you make a query. Something like this:

<?php
     include("conexion.php");         

    if(isset($_POST["cargar"])){
       $error=array();
    if(empty($_POST['campo1'])){
       $error[]='Por favor no dejes el campo de numero de CUIL en blanco';
   }else{
       $campo1=$_POST['campo1'];
   }
   if(empty($_POST['campo2'])){
      $error[]="No deje el campo de nombre en blanco";
   }else{
      $campo2=$_POST['campo2'];
   }
  if(empty($_POST['campo3'])){
      $error[]="No deje el campo de ID-NetBook en blanco";
  }else{
     $campo3=$_POST['campo3']; 
  }
     $campo4=$_POST['campo4'];
    $conexion=conectarse();
    if(empty($error)){

     //pregunta si hay registros,si no hay hace el insert
      $consultando_query="SELECT * FROM netbook_alumno WHERE cuil = 
      '$campo1'";
      $respuesta_query = $conexion->query($consultando_query);
      $contar=mysqli_num_rows($respuesta_query);
    //hace el insert
      if($contar==0){            
        $insertar_datos="INSERT INTO netbook_alumno 
     (cuil,nombre,id_netbook_alumno,observaciones)VALUES
        ('$campo1','$campo2','$campo3','$campo4')";
        $consulta_query2=$conexion->query($insertar_datos);
        echo"
        <script>alert('Datos Guardados');</script>";
          }else{
         /*ventana pop up*/
         print '<script>'; 
         print 'alert("Esa netbook ya existe.");'; 
         print '</script>';  
          }
      }else{
        foreach($error as  $values){
        echo '<li>'.$values.'</li>';

         }
      }
   }
   <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">

<title>Asignar NetBook a Alumno</title>
</head>
<body >

<h1>Cambios&nbsp; Stock&nbsp; Netbooks</h1>
 <form action="asignarAAlumno.php" name="mi_formulario" method="post">
 <p>
  <label for="textfield">CUIL:</label>
  <input type="text" name="campo1" id="campo1"  value= "<?php echo  $c1 ?>">
</p> 
<p>
 <label for="texfield">Nombre:</label>
 <input type="text" name="campo2" id="campo2" value = "<?php echo $c2 ?>">
</p>
<p>
  <label for="textfield">ID-NetBook:</label>
  <input type="text" name="campo3" id="campo3" value ="<?php echo $c3 ?>">
 </p>
 <p>
  <label for="textfield">Observaciones</label>
  <input type="text" name="campo4" id="campo4" value="<?php echo $c4 ?>">
 </p>
<p>
<input type="submit" name="cargar" id="button" value="Cargar">
</p>
</form>
<br>

<table border="3" >
    <tr  id="encabezado">
        <td>Cuil</td>
        <td>Nombre</td>
        <td>ID-NetBook</td>
        <td>Observaciones</td>

    </tr>

    <?php 

     $query2="SELECT * FROM netbook_alumno" ;//consulta para mostrar la db
     $resultado = $conexion->query($query2);
     mysqli_close($conexion);

     while($campo=mysqli_fetch_row($resultado)){

     ?>
    <tr>
        <td><?php echo $campo[0] ?></td>
        <td><?php echo $campo[1] ?></td>
        <td><?php echo $campo[2] ?></td>
        <td><?php echo $campo[3] ?></td>
    </tr>
   <?php 
     }
   ?>
      </table>
    </br>
  </body>
</html>
    
answered by 10.07.2018 / 18:04
source