Error while doing image update "Array to string Conversion"

Asked

Viewed 583 times

-1

I have a form where information is sent to the database, all are changed correctly except the image that is not changed.

ed_cons_.php

<?php
//inicia a conexao com o banco 

include ("conexao.php"); 
?>
<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
 //recebe o codigo da pag de consulta        
$iden = isset($_GET['iden'])?$_GET['iden']:"";      

$iden = $_GET ['codigo'];

// consulta no banco de dados         
$sql = "select * from eqp where codigo = '$iden'"; 
$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' enctype='multipart/form-data' action='salva_ed.php''>
            <td>Codigo:</td><td> <input type='text' name='codigo' value='"  .   $codigo . "'> </td>  

              <td>Arquivo em anexo: <input type='file' class= 'anexo' name='arquivo'> </td>

              <br><br>  

             </tr>

      <br><br>

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



 ");


        }

        ?>


</html>

salva_ed.php

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

 // recebe o codigo do registro 

$iden = isset($_POST['iden'])?$_POST['iden']:""; 

$iden = $_POST ['codigo']; 

$arquivo = isset($_FILES['arquivo'])?$_FILES['arquivo']:""; 
$arquivo = $_FILES ['arquivo']; 

// 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 =  mysqli_query("SELECT * FROM eqp WHERE codigo = '$iden' 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

   }
$new_name = $_FILES['arquivo'];
$imagem = $new_name;

var_dump($imagem);
echo '<pre>';
print_r($imagem);
echo '</pre>';
$query=("UPDATE eqp SET arquivo= '$imagem' WHERE codigo='$iden'");

  $result = mysqli_query($conexao,$query);

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

Mistake that occurred:

array(5) { ["name"]=> string(10) "images.jpg" ["type"]=> string(10) "image/jpeg" ["tmp_name"]=> string(24) "C:\xammp\tmp\phpE054.tmp" ["error"]=> int(0) ["size"]=> int(7225) }

Array
(
    [name] => images.jpg
    [type] => image/jpeg
    [tmp_name] => C:\xammp\tmp\phpE054.tmp
    [error] => 0
    [size] => 7225
)


Notice: Array to string conversion in C:\xammp\htdocs\agoravai\salva_ed.php on line 41
Registro Alterado com sucesso.

I have the function var_dump($imagem); which shows that he is receiving the image but does not alter in the bank, which may be causing this?

  • If $imagem is a array, which value should be the column arquivo when you do arquivo= '$imagem'? Can a bank column receive a array?

  • Hello Anderson, good morning and thank you for your attention! I didn’t understand very well the question you asked me haha but come on, good.. in my idea, $image would receive the name "file" that is coming from my query page to the edition page, so the $image variable receives the value of the image I am trying to send, and "file='$image' would be in case to be added to my photo in my database in the file field, because when it is sent by my form and added normally but when edited not only sends erases the image that already had in the database

1 answer

0


When you try to concatenate the $image variable inside the string of your UPDATE command it is not possible because $image is an array and not a string.

I believe you are trying to get the name of the image. In this case, in salva_ed.php, instead of $new_name = $_FILES['arquivo'];, for $_FILES['arquivo'] is an array, just take the name of the file that is within that array. So:

$new_name = $_FILES['arquivo']["name"];

Okay, now $new_name is a string that can be passed to $image and should no longer give concatenation problem in your UPDATE command.

  • Opaaaa, Thank you very much Denys, now this changing right, but the image does not appear when I do a search in my bank, how do I display it?

  • ´print "Attachment: <img src='upload/". $file." ' width=50px height=50px /><br/>"; use this command to display the image in the query, but after changing it does not show anything.

Browser other questions tagged

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