Update fields but keep previous image

See all posts See thread Reply

Re: Update fields but keep previous image new!
by colin, 6 years, 5 months ago
There are several things wrong with your code. If there is no uploaded files, $imagenNoticia is not empty. It is an array, with an error code. See http://php.net/manual/fr/reserved.variables.files.php

And then, you cannot store an upload object in $_POST['imagenNoticia-guardada']. Here, at best, you can keep the image file name, which you then will push back into your database.

Anyway, your issue is not related to the class itself, it is a PHP issue.Reply
Re: Update fields but keep previous image new!
by SoyDiego, 6 years, 5 months ago
Thanks for reply Colin.
In my form my field ìmagenNoticia_guardada is a hidden field and there i have my image before.

I have my previous code without your class using move_upload_files and works perfectly but i need use your class. I don't know how implement. I can implement your class in my INSERT but I can't in UPDATE.
Here is my previous code without class.upload.php

$idNoticia = $_POST['idNoticia'];
$imagenNoticia_guardada = $_POST['imagenNoticia-guardada'];
$imagenNoticia = $_FILES['imagenNoticia'];
$tituloNoticia = $_POST['tituloNoticia'];
$noticiaCorta = $_POST['noticiaCorta'];
$noticiaCompleta = nl2br($_POST['noticiaCompleta']);

if (empty($imagenNoticia['name'])) {
    $imagenNoticia = $imagenNoticia_guardada;
} else {
    $_FILES['imagenNoticia']['name'] = str_replace(" ", "-", $_FILES['imagenNoticia']['name']);
    $carpeta_destino = '../img/noticias/';
    $archivo_subido = $carpeta_destino . $_FILES['imagenNoticia']['name'];
    move_uploaded_file($_FILES['imagenNoticia']['tmp_name'], $archivo_subido);
    $imagenNoticia = $_FILES['imagenNoticia']['name'];
}

$statement = $conexion->prepare("UPDATE noticias SET tituloNoticia = :tituloNoticia, noticiaCorta = :noticiaCorta, noticiaCompleta = :noticiaCompleta, imagenNoticia = :imagenNoticia WHERE idNoticia = $idNoticia"
);

$statement->execute(array(
    ':tituloNoticia' => $tituloNoticia,
    ':noticiaCorta' => $noticiaCorta,
    ':noticiaCompleta' => $noticiaCompleta,
    ':imagenNoticia' => $imagenNoticia
));

I will understand if you don't want reply me. But i don't know where write because is my third post in differents site and nobody help me :(

Thanks and congratulations for the class, is amazing!Reply
Re: Update fields but keep previous image new!
by colin, 6 years, 5 months ago
Your code should be something like this:

$idNoticia = $_POST['idNoticia'];
$imagenNoticia_guardada = $_POST['imagenNoticia-guardada'];
$imagenNoticia = $_FILES['imagenNoticia'];
$tituloNoticia = $_POST['tituloNoticia'];
$noticiaCorta = $_POST['noticiaCorta'];
$noticiaCompleta = nl2br($_POST['noticiaCompleta']);

if (empty($imagenNoticia['name'])) {
    $imagenNoticia = $imagenNoticia_guardada;
} else {
    $upload = new Upload($imagenNoticia); 
    if ($upload->uploaded) {
      $upload->image_resize              = true;
      $upload->image_ratio               = true;
      $upload->image_x                   = 573; //ancho
      $upload->image_ratio_y             = true; //alto de acuerdo al ancho, mantiene perspectiva.
      $upload->image_watermark           = '../img/watermark.png';
      $upload->image_watermark_position  = 'TL';
      $upload->process('../img/noticias/');
      if ($upload->processed) {
        $imagenNoticia = $upload->file_dst_name;
      } else {
        // error!
      }
    } else {
      // error!
    }
}

$statement = $conexion->prepare("UPDATE noticias SET tituloNoticia = :tituloNoticia, noticiaCorta = :noticiaCorta, noticiaCompleta = :noticiaCompleta, imagenNoticia = :imagenNoticia WHERE idNoticia = $idNoticia");

$statement->execute(array(
    ':tituloNoticia' => $tituloNoticia,
    ':noticiaCorta' => $noticiaCorta,
    ':noticiaCompleta' => $noticiaCompleta,
    ':imagenNoticia' => $imagenNoticia
));
Reply
Re: Update fields but keep previous image new!
by SoyDiego, 6 years, 5 months ago
THANKS COLIN!!! You are amazing.
Now I will try learn more about this and my issue.

THanks for all and sorry for my problems and my english!

greetings from Argentina!Reply
Re: Update fields but keep previous image new!
by colin, 6 years, 5 months ago
My pleasure.Reply