Error returning mysql database data

Asked

Viewed 490 times

1

I made a normal form that returns the data in HTML. So I decided to insert images in the database and then gave a warning:

"Notice: Array to string Conversion in E: sites htdocs progdesenv2 admin acao.php on line 14"

As far as I understand it, are you saying it’s a text-to-text conversion array right? So I was left with some questions:
1 - How do I use text and image together??
2 - When I insert the data into the form it only brings in the HTML of the last record I entered but I wanted it to bring all the database data (like a table).
3 - In my last post I was told to use msqli instead of msql but I tried to use and there were many errors so I took it, there is some right way to use or just put the "i" in front of msql?
4 - I searched on the internet and put in the field of image that it is type LONGBLOB is the best alternative?

BRING BACK THE DATABASE

<?php
include "conexao.php";
$busca = mysql_query("SELECT * FROM formulario  ORDER BY nome ASC");
if (mysql_num_rows($busca))
{

    while ($resultado = mysql_fetch_array($busca))
    {
        extract($resultado);
        $l1 = $nome;
        $l2 = $idade;
        $l3 = $editor1;
        $l4 = $imagem;
    }       
}

?>

INSERTS THE DATA INTO THE DATABASE

<?php
include "conexao.php";
if(isset($_POST['acao']) && $_POST['acao'] == 'enviado'){

        $nome = $_POST['nome'];
        $idade = $_POST['idade'];
        $telefone = $_POST['telefone'];
        $editor1 = $_POST['editor1'];
        $imagem = $_FILES['imagem'];

        if(empty($nome) || empty($idade) || empty($telefone) || empty($editor1) || empty($imagem)){
                echo "Preencha os campos corretamente";
            }else{
                $insereDados = mysql_query("INSERT INTO formulario (nome, idade, telefone, editor1, imagem) VALUES ('$nome', '$idade', '$telefone', '$editor1', '$imagem' )");  
                echo "Enviado com sucesso!!";
            }
    }
?>

HTML

<section class="formulario">
  <form action="acao.php"  method="post" enctype="multipart/form-data">
    <input type="text" name="nome" placeholder="Nome:"><br>
    <input type="text" name="idade" placeholder="Idade:"><br>
    <input type="text" name="telefone" placeholder="Telefone:"><br>
    <input name="imagem" type="file"/><BR>
    <div class="tarea"><textarea class="ckeditor" name="editor1" cols="30" rows="10" placeholder="Mensagem:" ></textarea></div>
    <input type="hidden" name="acao" value="enviado">
    <input type="submit" value="Enviar Informações">
  </form>
</section>

I updated the code below and now there is no error only that in place of the image it brings written array what can be?:

<div>
<hgroup><h2>NOME</h2><h2>IDADE</h2><h2>MENSAGEM</h2><h2>IMAGEM</h2></hgroup>
    <?php
    include "conexao.php";
    $busca = mysql_query("SELECT * FROM formulario  ORDER BY nome ASC");
    while ($resultado = mysql_fetch_array($busca)){

     echo '<div>'.$resultado['nome'].'</div>';
     echo '<div>'.$resultado['idade'].'</div>';
     echo '<div>'.$resultado['editor1'].'</div>';
     echo '<div>'?><img src ="imagem/<?php echo $resultado['imagem']?> <?php echo '</div>';
} ?>
</div>  
  • gives a print_r or var_dump in $image to see the type of the variable

  • Another thing that by your script is saving an array of $_FILE and not the contents of the image

  • Line 14 is which?

  • @rray line 14 is this $inserts = mysql_query("INSERT INTO formulary (name, age, phone, editor1, image) VALUES ('$name', '$age', '$phone', '$editor1', '$image' )");

  • That answer might help you how to use the mysqli in the right way

1 answer

1

After entering the record, query and display all the results with the formatting inside the while and not outside.

You wrote the values after the while it means that only the last result will be displayed, the use of the Extract does not seem necessary. Do so to get the desired result

<?php while ($resultado = mysql_fetch_array($busca)){ ?>
<div>
    <hgroup><h2>NOME</h2><h2>IDADE</h2><h2>MENSAGEM</h2><h2>IMAGEM</h2></hgroup>
    <div><?php echo $resultado['nome'];?></div>    
    <div><?php echo $resultado['idade'];?></div>
    <div><?php echo $resultado['editor1'];?></div>
    <div><?php echo $resultado['imagem'];?></div>
</div> <br>
<?php } ?>
  • i did this as I said I appeared a Warning and a notice: Notice: Undefined variable: search in E: sites htdocs progdesenv2 admin index.php on line 35 Warning: mysql_fetch_array() expects Parameter 1 to be Resource, null Given in E: sites htdocs progdesenv2 admin index.php on line 35 line 35: <?php while ($result = mysql_fetch_array($search)){ ?>

  • @Estenioribeironobrega the question codes are in a file only?

  • are not separate, the only code that is bundled with HTML is the one that brings the database data

  • as it is in the post edited above I managed to bring the text but is still giving problem to upload the image, some idea of what is?

  • I’ll edit the answer when I get home.

  • i updated the code and as I said above it does not give error but appears written array in place of the image, like I do not have to echo inside an img tag???

Show 1 more comment

Browser other questions tagged

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