PHP edit image

Asked

Viewed 939 times

0

My problem is that the file img_editar.php 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 the edit option and when I change for example the name of the image I am obliged to change the images, only that changing 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’s 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>';
}
?>

1 answer

1

See, I put one if to check whether the array containing the image name exists and whether it returns something other than empty. If it returns it is because the user has also sent an image in addition to the name, description etc.. Then it adds a variable with the content to make it add an image update to the $result and already uploads the image.

<?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>';
}
?>
  • does not work continues to do the same

  • 2

    @Alisson Acioli, it would be interesting for you to inform the change that has been made.

  • yes thanks for the explanation but it is not giving :

Browser other questions tagged

You are not signed in. Login or sign up in order to post.