Question

I have a page to update news but it doesn't work.. I have this code but it have an error in the variable $imagem.

    <?php

 session_start();

 require_once '../config.php';

 $id_nt = $_GET["id_nt_up"];

 $titulo     = $_POST["noticia_titulo"];
 $conteudo   = $_POST["noticia_conteudo"];
 $descricao   = $_POST["noticia_descricao"];

  $autorPub   = $_SESSION["Usuario"];
  $autorSen   = $_SESSION["Senha"];  

  $SQL_AU = mysql_query("SELECT id, Nome FROM utilizadores WHERE Usuario='$autorPub' and    Senha='$autorSen' ");

  while($ath = mysql_fetch_assoc($SQL_AU)){
        $autorN = $ath["Nome"];
        $idAutor = $ath['id'];
  }

 // Configuração da Imagem
 $imagem  = $_FILES["imagem-noticia"];
 $destino = "../img/noticias/".$imagem['name'];

 if(isset($_POST["submit"])){

     mysql_query("UPDATE noticias SET Titulo='$titulo', Conteudo='$conteudo', Descricao='$descricao', Username=$autorN, Imagem='".$imagem['name']."' WHERE id_noticia=$id_nt");
     if($imagem['type'] == "image/jpeg"){

        if($imagem['type'] == "image/jpeg"){
        move_uploaded_file($imagem['tmp_name'] , $destino);
     }else if($imagem['type'] == "image/png"){
        move_uploaded_file($imagem['tmp_name'] , $destino);
     }else if($imagem['type'] == "image/gif"){
        move_uploaded_file($imagem['tmp_name'] , $destino); 
     }

     }
}
?>      

When i try to update an news, i get this error.

Notice: Undefined index: imagem-noticia in C:\wamp\www\bootstrap-3.1.1-dist\admin\editar_noticia_sucess.php on line 35

The editar_noticia_sucess.php is this file above.

Regards, Luis Candeias

Was it helpful?

Solution

The notice you get should not prevent page from working. It is caused because you try to access uploaded file when there is no file uploaded (or not by using 'imagem-noticia' input). This can be fixed changing this:

$imagem  = $_FILES["imagem-noticia"];
$destino = "../img/noticias/".$imagem['name'];

To:

if (isset($_FILES["imagem-noticia"])) {
    $imagem  = $_FILES["imagem-noticia"];
    $destino = "../img/noticias/".$imagem['name'];
}

New code makes sure that file ais uploaded before trying to perform further operations with it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top