Very good, so far I am making a simple CRUD , in which I use PDO to use the database. I have built all the activities that the CRUD can do except the update one, since it is giving me problems that I can't find. Until now I have been able to believe that I have a problem with the product id, since I call it by GET from the main page "listado.php" but it does not update it through the form of the product that I want to update, which I use as a POST method.
I leave you here the main page, which as you can see I call the edit button to the product in question of its id, which takes me to another page where the form is found:
!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./css/bootstrap.min.css">
<link rel="stylesheet" href="./css/style.css">
<title>Proyecto DSW</title>
</head>
<body>
<h1>Gestión de productos</h1>
<div class="d-grid gap-2">
<a href="crear.php" class="btn btn-success btn-block boton">Crear producto</a>
</div>
<table class="table table-striped table-hover">
<tr class="table-dark">
<th class="detalle">Detalle</th>
<th class="codigo">Codigo</th>
<th>Nombre</th>
<th>Acciones</th>
</tr>
<?php
include "conexion.php";
$pdo = Conexion::conectar();
$sql = $pdo->query("SELECT * FROM productos ORDER BY id DESC");
// $conexion = $sql->fetchAll(PDO::FETCH_OBJ);
foreach ($sql as $resultado) {
echo "<tr>";
echo "<td><a href='detalle.php?id=".$resultado['id']."' class='btn btn-info btn-block botonInfo'>Detalle</a></td>";
echo "<td class='codigo'><b>".$resultado['id']."</b></td>";
echo "<td><b>".$resultado['nombre']."<b></td>";
echo "<td>
<a href='editar.php?id=".$resultado['id']."' class='btn btn-warning btn-block botonExtra'>Actualizar</a>
<a href='borrar.php?id=".$resultado['id']."' class='btn btn-danger btn-block botonExtra'>Borrar</a></td>";
echo "</tr>";
echo "</table";
}
Conexion::desconectar();
?>
<script src="./js/bootstrap.min.js"></script>
</body>
</html>
The other page in question is edit.php , which is where the problems are coming from:
<?php
require "conexion.php";
// $id = null;
if (!empty($_GET['id'])) {
$id = $_REQUEST['id'];
}
if (!empty($_POST)) {
$nombre = $_POST['nombre'];
$nombre_corto = $_POST['nombre_corto'];
$pvp = $_POST['pvp'];
$select = $_POST['familia'];
$descripcion = $_POST['descripcion'];
$pdo = Conexion::conectar();
$sql = "UPDATE productos SET nombre = ?, nombre_corto = ?, descripcion = ?, pvp = ?, familia = ? WHERE id = ?;";
$conexion = $pdo->prepare($sql);
// echo "$sql";
// echo "$conexion";
$conexion->execute([$nombre, $nombre_corto, $descripcion, $pvp, $select, $id]);
echo "$conexion";
$nuevaURL = "listado.php";
Conexion::desconectar();
header('Location: ' . $nuevaURL);
}
$pdo = Conexion::conectar();
$sql = "SELECT * FROM productos where id = ?;";
$conexion = $pdo->prepare($sql);
$conexion->execute([$id]);
$data = $conexion->fetch(PDO::FETCH_OBJ);
$nombre = $data->nombre;
$nombre_corto = $data->nombre_corto;
$pvp = $data->pvp;
$select = $data->familia;
$descripcion = $data->descripcion;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./css/style2.css">
<link rel="stylesheet" href="./css/bootstrap.min.css">
<title>Crear producto</title>
</head>
<body>
<h1>Actualizar Producto</h1>
<div class="grid estilodiv">
<form class="row g-3" method="post" action="editar.php" id="formularioActualizar" autocomplete="off">
<div class="col-md-6">
<label class="form-label">Nombre</label>
<input type="text" class="form-control" name="nombre" placeholder="Nombre" value="<?php echo !empty($nombre) ? $nombre : ''; ?>">
</div>
<div class="col-md-6">
<label class="form-label">Nombre Corto</label>
<input type="text" class="form-control" name="nombre_corto" placeholder="Nombre Corto" value="<?php echo !empty($nombre_corto) ? $nombre_corto : ''; ?>">
</div>
<div class="col-md-6">
<label class="form-label">Precio (€)</label>
<input type="number" class="form-control" name="pvp" placeholder="Precio" value="<?php echo !empty($pvp) ? $pvp : ''; ?>">
</div>
<div class="col-md-6">
<label class="form-label">Familia</label>
<select class="form-control" name="familia">
<?php
$pdoSelect = Conexion::conectar();
$sqlSelect = $pdoSelect->query("SELECT * FROM familias");
$sqlSelect->execute();
while ($data = $sqlSelect->fetch(PDO::FETCH_OBJ)) {
echo '<option value="' . $data->cod . '">' . $data->nombre . '</option>';
}
Conexion::desconectar();
?>
</select>
</div>
<div class="mb-3">
<label class="form-label">Descripción</label>
<textarea class="form-control" name="descripcion" cols="20" rows="10" placeholder="Ingrese una descripción..."><?php echo !empty($descripcion) ? $descripcion : ''; ?></textarea>
</div>
<!-- <input type='hidden' name='id' value=$id> -->
<div class="d-grid gap-2">
<button type="submit" class="btn btn-primary">Actualizar</button>
</div>
<div class="d-grid gap-2">
<button type="reset" class="btn btn-success">Limpiar</button>
</div>
<div class="d-grid gap-2">
<a href="listado.php" class="btn btn-secondary">Volver</a>
</div>
</form>
</div>
<script src="./js/bootstrap.min.js"></script>
<?php
// Conexion::desconectar();
?>
</body>
</html>
It gives me errors that the "id" variable is not defended, but I don't know how to define it, since I try to do it through an input hidden field , but I don't know how to do it. Thank you very much.
I leave you the error that shows me when making echo "$conexion";
the FATAL ERROR, it's simple, it's by using this line of code.