Hello I have little knowledge in PHP, but with this I have managed to make a small application with images in which you can modify, delete or insert into a database with PHP and PHPMyAdmin, but I have a slight problem when wanting to modify certain fields:
As you can see in the image, it shows me that there is already an image, and that I can change it, but if I only want to modify a field, for example the name "Slide1", it forces me to change the image by selecting a new one and that's what I have put with a REQUIERE in my HTML code, but if I remove the REQUIRE from the button with type file, it changes the name field for which I put it, but the image disappears:
That is, I insert it as null and it gives me the result above, what I need to know is that I have to put in my php code so that when the image already exists, I only change the fields that I modified. This is my code:
<?php
include("./process/conexion.php");
$id = $_REQUEST['id'];
$nombre = $_POST['nombre'];
$Imagen = addslashes(file_get_contents($_FILES['Imagen']['tmp_name']));
$descripcion = $_POST['descripcion'];
$query = "UPDATE tabla_imagen SET nombre='$nombre', Imagen='$Imagen', descripcion='$descripcion' WHERE id = '$id'";
$resultado = $conexion->query($query);
if ($resultado) {
header("Location: mostrar.php");
} else {
echo "No se modifico :(";
}
?>
The HTML code:
<?php
session_start();
if ($_SESSION["logueado"] == TRUE) {
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Modificar Imagen</title>
<link rel="stylesheet" href="">
</head>
<body>
<?php
include("./process/conexion.php");
$id = $_REQUEST['id'];
$query = "SELECT * FROM tabla_imagen WHERE id = '$id'";
$resultado = $conexion->query($query);
$row = $resultado->fetch_assoc();
?>
<center><br><br><br>
<form action="proceso_modificar.php?id=<?php echo $row['id']; ?>" method="POST" enctype="multipart/form-data">
<input type="text" REQUIRED name="nombre" placeholder="Nombre" value="<?php echo $row['nombre']; ?>"/><br><br>
<img height="150px" src="data:image/jpg;base64,<?php echo base64_encode($row['Imagen']); ?>"/><br><br>
<input type="file" name="Imagen"/><br>
<p><b>Nota: </b>La imagen se encuentra en la categoria <b><?php echo $row['categoria']; ?></b></p><br>
<textarea name="descripcion" rows="10" cols="40" placeholder=""><?php echo $row['descripcion']; ?></textarea><br>
<a href="mostrar.php">Regresar</a><br><br>
<input type="submit" value="Aceptar">
</form>
</center>
</body>
</html>
<?php
}else{
header("Location: login.php");
}
?>