Error in PHP Form

0

I have a little doubt I have been working on a form. The question is that when I load the page it evaluates everything to me and it shows me error but when I enter data it shows me the data that I request without errors.

<?php
    include "mysqlcon.php";
    $con = connect();
    $opcion = $_POST['opcion'];
    $datos = $_POST['datos'];
    if ($datos <> 0) {
        if ($opcion == 1) {
            $consulta = "SELECT * FROM info WHERE id LIKE $datos"; 
        }
        if($opcion == 2){
            $consulta = "SELECT * FROM info WHERE telefono LIKE $datos"; 
        }
        $resultado = mysqli_query($con, $consulta);
        $contador = 0;
        while ($misdatos = mysqli_fetch_assoc($resultado)){ $contador++;?>
            <tr>
                <th scope="row"><?php echo $contador; ?></th>
                <td><?php echo $misdatos["id"]; ?></td>
                <td><?php echo $misdatos["telefono"]; ?></td>
                <td><?php echo $misdatos["nombre"]; ?></td>
                <td><?php echo $misdatos["contacto"]; ?></td>
                <td><?php echo $misdatos["nombre"]; ?></td>
            </tr>
        <?php }
    }
}?>

I do not know what I'm failing, I already probe with a while but it did not work.

    
asked by angel ramirez 12.10.2018 в 19:42
source

2 answers

1

The error gives you, because when entering the page $ _POST ['option'] & $ _POST ['data'] are not defined. (This is why they are sent through the form)

For this I recommend you do the following.

Add a check to see if the values come from the form

<?php
    include "mysqlcon.php";
    $con = connect();
    if(isset($_POST['opcion']) && isset($_POST['datos']))
    {
       $opcion = $_POST['opcion'];
       $datos = $_POST['datos'];
       if ($datos <> 0) {
       if ($opcion == 1) {
         $consulta = "SELECT * FROM info WHERE id LIKE $datos"; 
       }
       if($opcion == 2){
          $consulta = "SELECT * FROM info WHERE telefono LIKE $datos"; 
       }
       $resultado = mysqli_query($con, $consulta);
       $contador = 0;
       while ($misdatos = mysqli_fetch_assoc($resultado)){ $contador++;?>

       <tr>
          <th scope="row"><?php echo $contador; ?></th>
          <td><?php echo $misdatos["id"]; ?></td>
          <td><?php echo $misdatos["telefono"]; ?></td>
          <td><?php echo $misdatos["nombre"]; ?></td>
          <td><?php echo $misdatos["contacto"]; ?></td>
          <td><?php echo $misdatos["nombre"]; ?></td>
       </tr>
    <?php }
    }
    } // Fin de la condición IF
 else{
     //Código adicional para cuando las variables POST no tengan valor
 }
?>

Take into account that when the POST variables are not defined, by your code the page can show it to you in white, for it you must print the table inside the ELSE, with the values that you consider

    
answered by 12.10.2018 / 20:02
source
0

First you can simplify your code by writing everything in a single PHP tag:

 echo " <tr>
               <th scope='row'>".$contador."</th>
               <td>".$misdatos['id']."</td>
               <td>".$misdatos['telefono']."</td>
               <td>".$misdatos['nombre']."</td>
               <td>".$misdatos['contacto']."</td>
               <td>".$misdatos['nombre']."</td>
             </tr>";

Then if it tells you that the index option is indefindo it is because it is not coming from the view where you submit: to debug you can do the following

 $opcion = $_POST['opcion'];
 $datos = $_POST['datos'];
 var_dump($opcion);exit(); // esto te mostrara lo que tiene esa variable
    
answered by 12.10.2018 в 20:00