0

I am learning to use php, html and xamp. what happens is that I have an error because does not recognize a statement I make on the page to get the list (query) of what you have the database. the problem is when on the other page it tells me that empleadoID is not defined, according to this the empleadoID is not defined and i don't know why

here is the list. this is where I define it in the last 2 echo:

 <?php
  include("basededatos.php");
  $conexionbd=conectar_bd();
  $query ="SELECT id,nombre,edad FROM empleado;"; 
  $resultado = mysqli_query($conexionbd,$query);
  mysqli_close($conexionbd);
  ?>
  <html>
  <head>
  </head>
  <body>
            <h1>Lista de empleados</h1>
            <ul>
                <?php
                   while ($registro = mysqli_fetch_assoc($resultado)) 
                   {
                    echo '<li>'.$registro['nombre'].' ('.$registro['edad'].'años)';
                    echo '<a href="modificarEmpleado.php?empleadoID ='.$registro['id'].'"> Modificar</a></li>';
                    echo '<a href="eliminarEmpleado.php?empleadoID ='.$registro['id'].'"> Eliminar</a></li>';
                   }
                ?>
            </ul>
  </body>
  </html>

This is where he tells me that he doesn't exist:

<?php
include("basededatos.php");
$conexionbd = conectar_bd();
$query = "SELECT id,nombre,edad FROM empleado Where id".$_GET['empleadoID']."LIMIT 1";
$resultado=mysqli_query($conexionbd,$query);
mysqli_close($conexionbd);
$registro = mysqli_fetch_assoc($resultado);
?>

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Modificar Empleado</title>
</head>
<body>
<form action="actualizarempleado.php" method="post" name="nuevoempleado">
    Nombre: <input type="text" name="empleado_nombre" value="<?php echo $registro['nombre']?>">
    <br>
    <br>
    Edad: <input type="text" name="empleado_edad" value="<?php echo $registro['edad']?>">
    <br>
    <br>
    <input type="hidden" name="empleado_id" value="<?php echo $registro['id']?>">
    <br>
    <br>
    <input type="submit" name="actualizar empleado">
</form> 

</body>
</html>

These are the errors in the second page:

 Notice: Undefined index: empleadoID in C:\xampp\htdocs\modificarempleado.php on line 5

 Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, bool given in C:\xampp\htdocs\modificarempleado.php on line 8
Dharman
  • 30,962
  • 25
  • 85
  • 135

1 Answers1

0

You have 2 major problems. First, you are not using prepared statements. Second, you have a typo error in your SQL query, missing =. You can't see the errors because you have not enabled MySQLi errors.

The correct approach would look similar to this

<?php
include "basededatos.php";
// This line should go inside conectar_bd()
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conexionbd = conectar_bd();

// prepared -> bind -> execute -> get result
$query = "SELECT id,nombre,edad FROM empleado Where id=? LIMIT 1";
$stmt = $conexionbd->prepare($query);
$stmt->bind_param('i', $_GET['empleadoID']);
$stmt->execute();
$resultado = $stmt->get_result();

mysqli_close($conexionbd);
$registro = mysqli_fetch_assoc($resultado);
?>
Dharman
  • 30,962
  • 25
  • 85
  • 135