Edit News and keep same image class.upload.php

0

I would like to know how I can solve this inconviente. I am using class class.upload.php.

When editing a news, I want that if I do not select a new photo in the input file, I KEEP THE PHOTO SAVED (that is, the first one that came up when loading that news). I get the UPDATE correctly when I edit all the fields including the image of the news, but what does not come out is when I only edit the input, without adding a new image.

My code is this:

elseif ($accion == 'modificar') {

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


    if (empty($imagenNoticia)) {
        $imagenNoticia = $imagenNoticia_guardada;
    }

    if ($imagenNoticia->uploaded) {
      $imagenNoticia->image_resize              = true;
      $imagenNoticia->image_ratio               = true;
      $imagenNoticia->image_x                   = 573; //ancho
      $imagenNoticia->image_ratio_y             = true; //alto de acuerdo al ancho, mantiene perspectiva.
      $imagenNoticia->image_watermark           = '../img/watermark.png';
      $imagenNoticia->image_watermark_position  = 'TL';

        $imagenNoticia->process('../img/noticias/');
        if ($imagenNoticia->processed) {

            $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->file_dst_name
            ));

            echo "La noticia ha sido editada correctamente.";
    }
}

The problem in my opinion is in this line that something I'm doing wrong, or I'm assigning it wrong.

    if (empty($imagenNoticia)) {
    $imagenNoticia = $imagenNoticia_guardada;
}

He shows me NO MESSAGE OF ANYTHING. I even add an echo inside that if and it does not show it either.

I hope you can help me, thank you very much!

    
asked by ZeR0ByTe 26.11.2017 в 22:57
source

1 answer

0

In my opinion according to the logic you have, you should have two queries: 1.if there is an image {update everything + image} 2. else {update everything except image}, because if you send an update to a field of the table without value will fail the query, and if you send "update = ''" emptied you will delete the previous name.

if (!empty($imagenNoticia)) {
  $imagenNoticia = $imagenNoticia_guardada;


  if ($imagenNoticia - > uploaded) {
    $imagenNoticia - > image_resize = true;
    $imagenNoticia - > image_ratio = true;
    $imagenNoticia - > image_x = 573; //ancho
    $imagenNoticia - > image_ratio_y = true; //alto de acuerdo al ancho, mantiene perspectiva.
    $imagenNoticia - > image_watermark = '../img/watermark.png';
    $imagenNoticia - > image_watermark_position = 'TL';

    $imagenNoticia - > process('../img/noticias/');
    if ($imagenNoticia - > processed) {

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

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

      echo "La noticia ha sido editada correctamente.";
    }
}else{
      $statement = $conexion - > prepare("UPDATE noticias SET tituloNoticia = :tituloNoticia, noticiaCorta = :noticiaCorta, noticiaCompleta = :noticiaCompleta WHERE idNoticia LIKE $idNoticia");

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

      echo "La noticia ha sido editada correctamente.";
}

I would also review, where the query "... WHERE idNoticioa LIKE $ idNoticia", for there are say that it is from programmers "if it works do not touch it" but if you put the LIKE instead of = ... / p>     

answered by 27.11.2017 в 14:48