Replace one image with another PHP and MYSQLI

0

I have my code to edit images in my database, but when replacing the image in the folder or the path where it is saved does not replace them, but adds a new one.

I leave image below:

The image that is underlined in yellow is the one I want to replace. And the image that is underlined with gray is the new image. But instead of replacing the old image with the new one it just adds it and already.

This is my code:

<?php 
session_start();

$conexion = mysqli_connect("localhost", "root", "", "trigoyponque2017");

$id             = $_GET['id'];
$titulo         = $_POST['titulo'];
$descripcion    = $_POST['descripcion'];

$foto1   = $_FILES["foto"]["name"];
$foto    = rand(0,9).rand(100,9999).rand(100,9999).".".$foto1;
$ruta    = $_FILES["foto"]["tmp_name"];
$destino = "../productos/".$foto;
move_uploaded_file($ruta,$destino);

if($_FILES["foto"]["name"]){
    $editar   = "UPDATE producto SET titulo= '$titulo', descripcion = '$descripcion', foto = '$foto' WHERE id= '$id'";
    echo "<script> alert ('Producto actualizado correctamente.'); window.location='editar_producto1.php?id=$id' </script>";
}else{
    $editar   = "UPDATE producto SET titulo= '$titulo', descripcion = '$descripcion' WHERE id= '$id'";
    echo "<script> alert ('Producto actualizado correctamente.'); window.location='editar_producto1.php?id=$id' </script>";
}



$resultado = mysqli_query($conexion,$editar);

if(!$resultado){
    echo "<script> alert('Error.'); window.location='editar_producto1.php?id=$id'</script>";
}else{
    echo "<script> alert('Producto actualizado correctamente.'); window.location='editar_producto1.php?id=$id'></script>";
}

mysqli_close($conexion);
?>

PS: I must clarify that I am saving the photos with random numbers as a kind of ID.

Image in Database:

    
asked by Sercroft1 31.10.2017 в 22:07
source

1 answer

1

That happens because in no time you are changing the photo in the new folder, you are always adding it, then you must first select the current photo, delete it and then continue with your normal flow, something like this:

$foto1   = $_FILES["foto"]["name"];
$foto    = rand(0,9).rand(100,9999).rand(100,9999).".".$foto1;
$ruta    = $_FILES["foto"]["tmp_name"];
$destino = "../productos/".$foto;

$selecionar = "SELECT foto FROM producto WHERE id = '$id'";
$resultado_seleccionar = mysqli_query($conexion, $selecionar);

$foto_db = mysqli_fetch_array($resultado_seleccionar);
$ruta_foto_db = "../productos/" . $foto_db['foto'];

if(file_exists($ruta_foto_db)){
    unlink($ruta_foto_db);
}

if (!file_exists($destino)) {
    move_uploaded_file($ruta,$destino);
}

if($_FILES["foto"]["name"]){
    $editar   = "UPDATE producto SET titulo= '$titulo', descripcion = '$descripcion', foto = '$foto' WHERE id= '$id'";
    echo "<script> alert ('Producto actualizado correctamente.'); window.location='editar_producto1.php?id=$id' </script>";
}else{
    $editar   = "UPDATE producto SET titulo= '$titulo', descripcion = '$descripcion' WHERE id= '$id'";
    echo "<script> alert ('Producto actualizado correctamente.'); window.location='editar_producto1.php?id=$id' </script>";
}
    
answered by 31.10.2017 в 22:24