Something is happening to me that I can not understand. I have a form that has two inputs of the button type, apart from other fields to fill. These two buttons have two different actions, one to show a preview and another to save changes to the article.
The summary form would be like this:
<form class="inicio_sesion" enctype="multipart/form-data" action="<?php echo $ruta ?>editar_articulo.php?ID=<?php echo $id ?>" method="post">
<input type="hidden" name="id" value="<?php echo $post['ID'] ?>"
<input class="submit" type="submit" name="enviar" value="Guardar cambios">
<input class="submit" type="submit" name="previa" value="Vista previa">
</form>
On the other hand, I receive the information in PHP:
if (isset($_POST['enviar']) AND $conexion) {
$creador = isset($_POST['creador']) ? $_POST['creador'] : 'Egoi Cantero';
$categoria = isset($_POST['categoria']) ? $_POST['categoria'] : 'Articulo';
$juego = isset($_POST['juego']) ? $_POST['juego'] : 'Ninguno';
$plataforma = isset($_POST['plataforma']) ? $_POST['plataforma'] : 'Xbox One';
$titulo = trim($_POST['titulo']);
$publicado = $creador;
$id = $_POST['id'];
$categoria = $categoria;
$thumb = $_FILES['thumb'];
$articulo = $_POST['articulo'];
$etiquetas = $_POST['etiqueta'];
echo $articulo;
$hay_ya_foto = false;
//miramos si la foto ya se ha subido, si el hidden de foto guardada tiene un valor es que ya se ha subido
//cogemos el valor de ese input para meterlo en la base de datos
if (empty($thumb_db)) {
$thumb = $_POST['foto_hidden'];
$hay_ya_foto = true;
}
if (empty($titulo)) {
$errores .= "El título es obligatorio </br>";
} else {
if (strlen($titulo) > 90) {
$errores .= "El título no puede tener más de 70 caracteres </br>";
}
}
if (empty($publicado)) {
$errores .= "Es obligatorio indicar el autor del artículo </br>";
}
if (empty($publicado)) {
$errores .= "Por favor introduce etiquetas para catalogar el artículo </br>";
}
if (empty($articulo)) {
$errores .= "El artículo está vacío </br>";
}
if (empty($errores)) {
echo $articulo;
$statement = $conexion->prepare("UPDATE art SET
plataforma = :plataforma,
juego_asociado = :juego_asociado,
titulo = :titulo,
thumb = :thumb,
publicado_por = :publicado,
etiquetas = :etiquetas,
categoria = :categoria,
articulo = :articulo
WHERE id = $id");
echo $articulo;
$statement->execute(array(
':plataforma' => $plataforma,
':juego_asociado' => $juego,
':titulo' => $titulo,
':thumb' => $thumb,
':publicado' => $publicado,
':etiquetas' => $etiquetas,
':categoria' => $categoria,
':articulo' => $articulo
));
//header("Location: " . $ruta . "index_admin.php");
}
}
If I am going to edit the article and I do not click on preview, all the information is updated well in the database, but if before saving the changes I give the preview button, the information is not updated.
I give an example of what is happening to me. I imagine that I have the text of the article as "Hello World!". I have two echo $articulo
in the code to see what is its value, just after picking it up by POST and just before entering it in the database, and curiously the value of the variable $articulo
is correct, but in the base of data does not update me anything.
Neither does it throw me any error, I have checked all the data with echo and they are fine, I do not understand why the data is not updated if instead of changing the article and saving it I give it to preview. I clarify that it does not update any of the fields.