Replace an old image with php and MySqli

0

Good evening!

I know how to add an image and save it in my Mysql DB, but at the time of doing the UPDATE the image does not replace it.

<?php 
session_start();

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

$id             = $_GET['id'];
$titulo         = $_POST['titulo'];
$descripcion    = $_POST['descripcion'];
$foto           = $_FILES["foto"]["name"];
$ruta           = $_FILES["foto"]["tmp_name"];
$destino        = "../productos/".$foto;
copy($ruta,$destino);

if($foto != ""){
    $editar   = "UPDATE producto SET titulo= '$titulo', descripcion = '$descripcion' WHERE id= '$id'";
}else{
    $editar   = "UPDATE producto SET titulo= '$titulo', descripcion = '$descripcion', foto = '$destino' WHERE id= '$id'";
}


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

if(!$resultado){
    echo "ERROR.";
}else{
    echo "Producto editado correctamente.";
}

mysqli_close($conexion);
?>

Only the route is visible in my database, but not the photo that has been uploaded.

    
asked by Sercroft1 25.10.2017 в 06:35
source

1 answer

0

Perform a var_dump of $ _FILES ['photo'] why it seems to be undefined, it is possible that the photo is not uploading or the input is not called a photo.

On the other hand, do not use copyright to move the file you use: move_uploaded_file.

Also valid before $ _FILES ['photo'] with isset and make sure that the name is only the file name and not the entire path.

    
answered by 25.10.2017 в 06:52