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.
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.