Difficulty changing image in the database HTML/PHP Form

Asked

Viewed 392 times

0

Talk good to you guys ?

I was wondering if someone could give me a light at the end of the hehe tunnel, so let’s go!

I have an HTML form where the information is sent to the database and then displays it in a query, until ai blz, but when I change the data, it changes all the information, except the image, I saw in some forums that would have to delete the image before updating in the database, but I still can’t get this feat, it even deletes the image, however the update does not only insert as if it were a new record. I’ll post the codes here, if anyone can help correct the mistakes I thank you from now, hugs!!


inicio.php

 <form method="post" action="processa.php" enctype="multipart/form-data">
<div class="form-group">

            <input type="submit" class="bnt salvar" value="Enviar"> 
  <label for="inputEmail3" class="col-sm-2 control-label">Imagem:</label>
  <div class="col-sm-10">
   <h5>Arquivo em anexo: <font color="#00BFFF">*</font></h5> <input type="file" class= "anexo" name="arquivo">
  </div>
  </div>
</form>

edit.php

<?php
include ("conexao.php"); //inicia a conexao com o banco 
?>
<html lang ="pt-br">      
<html>
  <head>                     
    <meta charset="utf-8"> 
    <title>Formulario</title>
    <link rel="stylesheet" href="../agoravai/css/estilo.css">
  </head>       
            <body>

                    <nav>


                            <ul class="menu">
                            <a href="../agoravai/inicio.php"><li>Inicio</li></a>
                             <a href="../aff/consultaeqp.php"><li>Consulta</a>

                               </ul>

                   </nav>



              </body>




        <?php

$iden = isset($_GET['iden'])?$_GET['iden']:""; //recebe o codigo da pag de consulta      

$iden = $_GET ['codigo'];


$sql = "select * from eqp where codigo = '$iden'"; // consulta no banco de dados 
$consulta = mysqli_query($conexao,$sql);
$registros = mysqli_num_rows($consulta);
while($linhas = mysqli_fetch_array($consulta)){

//recebe os dados    

$codigo = $linhas ['codigo'];
$arquivo = $linhas['arquivo'];



            echo ("


            <tr>

              <form method='post' action='salva_ed.php''>
            <td>Codigo:</td><td> <input type='text' name='codigo' value='"  .   $codigo . "'> </td>  

              <td>anexo:</td><td> <input type='file'name='imagem'></td>

              <br><br>  

             </tr>

      <br><br>

      <input type='submit' class='bnt salvar' value='Salvar'> 



 ");


        }

        ?>


</html>

salva_ed.php


<?php
session_start();
include ("conexao.php"); //inicia conexão com o banco 

$arquivo= $_FILES['imagem']['name']; // recebe a imagem da pagina de edição 
$iden = isset($_POST['iden'])?$_POST['iden']:"";  // recebe o codigo do registro    

$iden = $_POST ['codigo']; // codigo registro 

// aqui seria onde ele veifica se existe algo no campo arquivo e o substitui por um valor em branco 
if($arquivo == ""){
    $query =("update eqp set arquivo = '' WHERE codigo='$iden'");
}else{

// aqui seria onde fazia o update da imagem, dando um novo nome e movendo para a pasta de upload 
 if(isset($_FILES['imagem']))
   {
      $sql = mysql_query("SELECT * FROM eqp WHERE codigo = '$id' LIMIT 1");
      $resultado = mysql_fetch_assoc($result);

    date_default_timezone_set("Brazil/East"); //Definindo timezone padrão
    $ext = strtolower(substr($_FILES['imagem']['name'],-4)); //Pegando extensão do arquivo
    $new_name = $resultado['foto']; //Definindo um novo nome para o arquivo
    $dir = 'upload'; //Diretório para uploads

    move_uploaded_file($_FILES['imagem']['tmp_name'], $dir.$new_name); //Fazer upload do arquivo

   }

$imagem = $new_name;

$query = mysql_query("UPDATE eqp set arquivo = '$imagem' WHERE codigo='$id'");

  $result1 = mysqli_query($conexao,$sql,$query);

  // Verifica se o comando foi executado com sucesso
  if(!$result1)
    echo "Registro NÃO alterado.";
  else
    echo "Registro Alterado com sucesso.";
}
?>

  • Codes without borders. Action of the first form processa.php. Action of the second form salva_ed.php and neither of these two files exists!

  • Hello leo, good morning, the first processing, would be from my home page. Where all the information is sent (there are more fields that are sent but I only left the image to be easier), and in the second form, it would be where I already took the data from the database to edit and send to the salva_ed.php where this page is giving the error "Notice: Undefined index: image in C: xammp htdocs

No answers

Browser other questions tagged

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