I get this error: Notice: Undefined index: id in C: \ xampp \ htdocs \ master \ modify.php on line 4

1

Good afternoon my name is Any and I am programming a database to do some surveys and I get this error:

  

Notice: Undefined index: id in C: \ xampp \ htdocs \ master \ modify.php   on line 4

Here is the code sheet modify

<?php
    require 'conexion.php';

    $id = $_GET['id'];                                                                                                                                                  

    $sql = "SELECT * FROM encuestador WHERE id_encuestador = '$id'";
    $resultado = $mysqli->query($sql);
    $row = $resultado->fetch_array(MYSQLI_ASSOC);

?>
<html lang="es">
    <head>

        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link href="css/bootstrap.min.css" rel="stylesheet">
        <link href="css/bootstrap-theme.css" rel="stylesheet">
        <script src="js/jquery-3.1.1.min.js"></script>
        <script src="js/bootstrap.min.js"></script> 
    </head>

    <body>
        <div class="container">
            <div class="row">
                <h3 style="text-align:center">MODIFICAR REGISTRO</h3>
            </div>

            <form class="form-horizontal" method="POST" action="update.php" autocomplete="off">
                <div class="form-group">
                    <label for="Identificacion" class="col-sm-2 control-label">Identificacion</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="Identificacion" name="Identificacion" placeholder="Identificacion" value="<?php echo $row['Identificacion']; ?>" required>
                    </div>
                </div>

                <div class="form-group">
                    <label for="Nombre y Apellido" class="col-sm-2 control-label">Nombre Funcionario</label>
                    <div class="col-sm-10">
                        <input type="Nombre y Apellido" class="form-control" id="NomApeFuncionario" name="NomApeFuncionario" placeholder="NomApeFuncionario" value="<?php echo $row['NomApeFuncionario']; ?>"  required>
                    </div>
                </div>

                <div class="form-group">
                    <label for="Entidad" class="col-sm-2 control-label">Entidad</label>
                    <div class="col-sm-10">
                        <input type="Entidad" class="form-control" id="Entidad" name="Entidad" placeholder="Entidad" value="<?php echo $row['Entidad']; ?>" >
                    </div>
                </div>
                    </div>
                </div>
                    </div>
                </div>

                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <a href="index.php" class="btn btn-default">Regresar</a>
                        <button type="submit" class="btn btn-primary">Guardar</button>
                    </div>
                </div>
            </form>
        </div>
    </body>
</html>

Thanks for the help in advance !!

    
asked by any 26.06.2018 в 22:14
source

2 answers

3

As the message indicates it id is not defined, just check that what comes from the url is defined with isset()

if(isset($_GET['id'])){
 $id = $_GET['id'];                                                                                                                                                  

$sql = "SELECT * FROM encuestador WHERE id_encuestador = '$id'";
$resultado = $mysqli->query($sql);
$row = $resultado->fetch_array(MYSQLI_ASSOC);
}
    
answered by 28.06.2018 в 23:40
1

The variable $_GET is simply trying to read a variable in the URL, something like modificar.php?id=xx . The php must have activated the highest error level ( display_error=ALL in php.ini), and that's why this warning is being displayed.

Anyway seeing your query, this id is mandatory, so you should check first that the id is passing by URL, or otherwise show a message saying that it is necessary or something similar. It would be something like:

<?php
require 'conexion.php';

if (empty($_GET['id'])) {
   echo "ID es requerido.";
} else {
   $id = $_GET['id'];                                                                                                                                                  

   $sql = "SELECT * FROM encuestador WHERE id_encuestador = '$id'";
   $resultado = $mysqli->query($sql);
   $row = $resultado->fetch_array(MYSQLI_ASSOC);
}
?>

Possibly forward to another screen or show an error or something like that. It depends on the logic you are implementing.

    
answered by 27.06.2018 в 00:34