PHP edit image

0

My problem is that the file img_editar.php is not sending the images to the database (it is to add several images at the same time), the file add image works very well, I click on the edit option and when I change for example the name of the image I'm forced to change the images, but to change the images these are not sent to the BD, I even have the function to keep the images if($_FILES['img']['size'] > 0) but it is not working.

<?php
    include "bd.php";
    include "_config.php";
    include "admin.php";

  $target = "../upload/"; 
  $target = $target . basename( $_FILES['img']['name']);
  $pic=($_FILES['img']['name']);


 $data=date("ymd");
$query="update portfolio_imagens set nome='".$_POST['nome']."', descricao='".$_POST['descricao']."', cliente='".$_POST['cliente']."', publicacao='".$data."' where id=".$_GET['id'];
    $result=mysql_query($query);
 $picas=0;
    echo $query;
    if($_FILES['img']['size'] > 0){
      //echo "1";
  $query2="update portfolio_imagens set  imagem='".$pic."' where id=".$_GET['id'];
  $result2=mysql_query($query2);
  $picas=1;
}

  if(move_uploaded_file($_FILES['img']['tmp_name'], $target) || $picas=1) // 
  { 

 echo "<div class=\"alert alert-success fade in\">

          <i class=\"fa fa-check-circle fa-fw fa-lg\"></i>
          <strong>Notícia criada com sucesso!</strong> 
         </div>";
         echo '<script>window.location.href = "adicionar.php?msg=1";</script>';

 }else{
  echo "<div class=\"alert alert-danger fade in\">

          <i class=\"fa fa-ban fa-fw fa-lg\"></i>
          <strong>Erro!</strong> 
         </div>";
         echo '<script>window.location.href = "adicionar.php?msg=2";</script>';
}
?>
    
asked by anonymous 15.09.2014 / 10:27

1 answer

1

See, I've put if to check if the array containing the image name exists and whether it returns anything other than empty. If it returns it is because the user has also uploaded an image besides the name, description etc .. Then it adds a variable with the content to make it add an update of the image in $result and already upload the image. p>

<?php

include "bd.php";
include "_config.php";
include "admin.php";

$target = "../upload/"; //Diretório aonde irá salvar
$target = $target . $_FILES['img']['name']; //Caminho completo
$pic= $_FILES['img']['name']; //Pega nome do arquivo com extensão

$ID = $_GET['id']; //ID

if(isset($_FILES['img']['name']) && !empty($_FILES['img']['name'])){ //Verifica se existe o array name e se ele é diferente de vazio

  $condition_aditional = ", imagem='".$pic."'"; //Linha adicional no UPDATE
  move_uploaded_file($_FILES['img']['tmp_name'], $target); //Move o arquivo de foto
}


$result=mysql_query("UPDATE portfolio_imagens SET nome='".$_POST['nome']."', descricao='".$_POST['descricao']."', cliente='".$_POST['cliente']."', publicacao='".$data."' ".$condition_aditional." WHERE id=".$ID.""); //Executa a Query

  if($result){ 

 echo "<div class=\"alert alert-success fade in\">

          <i class=\"fa fa-check-circle fa-fw fa-lg\"></i>
          <strong>Notícia criada com sucesso!</strong> 
         </div>";
         echo '<script>window.location.href = "adicionar.php?msg=1";</script>';

 }else{
  echo "<div class=\"alert alert-danger fade in\">

          <i class=\"fa fa-ban fa-fw fa-lg\"></i>
          <strong>Erro!</strong> 
         </div>";
         echo '<script>window.location.href = "adicionar.php?msg=2";</script>';
}
?>
    
15.09.2014 / 11:48