I have a code to be able to edit articles and that when saving them, the database is updated. The HTML code:
<form class="articulos" enctype="multipart/form-data" action="editar.php" method="post">
<input type="hidden" name="id" value="<?php echo $post['ID'] ?>">
<h1>Título</h1>
<input type="text" name="titulo" value="<?php echo $post['titulo'] ?>">
<h1>Publicado por:</h1>
<select class="select" name="creador">
<option name="Egoi" value="Egoi" <?php if ($creador == 'Egoi Cantero') {echo 'selected';} ?>>Egoi Cantero</option>
</select>
<h1>Categoria:</h1>
<select class="select" name="categoria">
<option name="Articulo" value="Articulo" <?php if ($categoria == 'Articulo') {echo 'selected';} ?>>Articulo</option>
<option name="Analisis" value="Analisis" <?php if ($categoria == 'Analisis') {echo 'selected';} ?>>Análisis</option>
</select>
<h1>Artículo</h1>
<textarea name="articulo"><?php echo $post['articulo'] ?></textarea>
<input class="fichero" type="file" name="thumb">
<input class="enviar" type="submit" name="enviar" value="Guardar cambios">
</form>
The PHP code, the first thing it does is load the information through the ID.
<?php
$id = $_GET['ID'];
try {
$conexion = new PDO("mysql:host=localhost;dbname=Blog", 'root', '' );
} catch (Exception $e) {
echo "Error: " . $e->getMessage;
die();
}
if (isset($_POST['enviar']) AND $conexion) {
$creador = isset($_GET['creador']) ? $_GET['creador'] : 'Egoi Cantero';
$categoria = isset($_GET['categoria']) ? $_GET['categoria'] : 'Articulo';
$titulo = trim($_POST['titulo']);
$publicado = $creador;
$id = trim($_POST['ID']);
$categoria = $categoria;
$thumb = $_FILES['thumb']['tmp_name'];
$thumb_db = $_FILES['thumb']['name'];
$articulo = $_POST['articulo'];
$ruta = '../imagenes/articulos/' . $thumb_db;
$statement = $conexion->prepare("UPDATE art SET
titulo = :titulo,
thumb = :thumb,
publicado_por = :publicado,
categoria = :categoria,
articulo = :articulo
WHERE id = $id");
$statement->execute(array(
':titulo' => $titulo,
':thumb' => $thumb_db,
':publicado' => $publicado,
':categoria' => $categoria,
':articulo' => $articulo
));
header('panel_control.php');
echo "hola";
} else {
$statement = $conexion->prepare("SELECT * FROM art WHERE id = $id");
$statement->execute();
$posts = $statement->fetchAll();
$post = $posts[0];
$creador = $post['publicado_por'];
$categoria = $post['categoria'];
}
require '../views/editar.view.php';
?>
I just did not find the error. Also, do not redirect me to " panel_control.php " or show me "hello" on the screen, I've put it to know if I was entering that part of the code.
Changing the line of code
$id = $_POST['ID'];
By
$id = $_POST['id'];
The header works, that is, it redirects me, although the entry in the database is not updated.