Verification of an existing data

0

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");
    }
 ?>
    
asked by Santiago Huh 20.06.2017 в 18:27
source

1 answer

1

In your file "process_modify.php" you should verify that the array $_FILES actually has something inside. A possible solution is the following:

Extract all the information from the array POST

$id = $_REQUEST['id'];
$nombre = $_POST['nombre'];
$descripcion = $_POST['descripcion'];

Afterwards, you can check if the $_FILES fix actually has something inside. If you do not have it, it is because no photograph has been included and therefore, the database in that field should not be updated. Something like the following:

if($_FILES!=null)
{
    $Imagen = addslashes(file_get_contents($_FILES['Imagen']['tmp_name']));
    $query = "UPDATE tabla_imagen SET nombre='$nombre', Imagen='$Imagen', descripcion='$descripcion' WHERE id = '$id'";
} 
else
{
    $query = "UPDATE tabla_imagen SET nombre='$nombre', descripcion='$descripcion' WHERE id = '$id'";
}

This way you can make sure that only the image will be updated when the user selects a new image. I also recommend you see this question to learn a bit about how to protect yourself when making insertions to the database.

    
answered by 20.06.2017 / 19:06
source