ERROR while retrieving data from a form through $_POST[] in PHP

Asked

Viewed 373 times

1

I created a simple form:

<form  action="php/cadastro.php" method="post" enctype="multipart/form-data">
    <fieldset>
        <legend>Autor:</legend>
        <input type="text" name="autor" placeholder="Informe seu nome" required="required">
    </fieldset>
    <fieldset>
        <legend>Título:</legend>
        <input type="text" name="titulo" placeholder="Informe um título para o post" required="required">
    </fieldset>
    <fieldset>
        <legend>Descrição:</legend>
        <p>
            <textarea name="materia" placeholder="Digite o poste aqui..." required="required"></textarea>
        </p>
    </fieldset>     
    <fieldset>     
        <legend>Imagem:</legend>
        <input type="file" name="foto" required="required">
    </fieldset>
    <fieldset>
        <input type="submit" value="Postar" class="botao">
        <input type="submit" value="Cancelar" class="botao">
    </fieldset>
</form>

However, at the time of recovering the data to my script displays error:

Undefined index: foto in C: wamp www comicnews php cadastro.php on line 6

Exit from var_dump:

array (size=3)
  'autor' => string 'Ademir Santos' (length=13)
  'titulo' => string 'Deadpool o maior heroi da terra' (length=31)
  'materia' => string 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer tortor justo, elementum vel tincidunt vitae, ullamcorper sed dolor. Etiam leo sapien, faucibus sit amet porttitor non, fermentum vitae mi. Curabitur ac velit ut ante bibendum consequat a et sem. Quisque pellentesque libero lacus, sed finibus purus iaculis vel.' (length=325)

I notice you are not finding the index "photo" in the array $_POST['foto'], the question is why? How to solve?

Here is the PHP script:

<?php

include_once  'conexao.php';

$autor=$_POST['autor'];
$imagem= $_POST['foto'];
$titulo=$_POST['titulo'];
$materia= $_POST['materia'];


var_dump($_POST);   

//insere no BD os dados da matéria e do autor, assim também como o caminho da imagem que ele deseja publicas
$query= "insert into noticia(titulo, descricao, img, autor) values ('{$titulo}','{$materia}','wamp/www/comicsnews/imagens/.{$imagem}','{$autor}')";

//Executa a query e mostra o resultado
$resultado = mysqli_query($conexao, $query);

if($resultado){

    echo "sucesso";

}else{

    echo "<br>Não salvou". mysqli_error($conexao);
}

I’d appreciate it if you could help.

  • also put the opening of your tag <form>, you’re probably forgetting the enctype.

  • Amigo...the image you will access from $FILES

  • thanks, Israel that even accesses by $_FILES, did not know that, thanks Rafael, but it turns out that it was there only that the ctrlV crtlC did not take, but anyway I managed to solve the problem and it was why you need to access with $FILES same, thankful.

  • solved, thanks for the help! the files must be accessed by $_FILE[].

1 answer

1

To send image files you need to change the FORM:

<!-- O tipo de encoding de dados, enctype, DEVE ser especificado abaixo --> 
<form  enctype="multipart/form-data"  action="__URL__" method="POST">

        Enviar esse arquivo: <input name="userfile" type="file" />
        <input type="submit" value="Enviar arquivo" /> </form>

Note that the form needs to have the enctype enctype="multipart/form-data"

On the PHP side you access it as follows: $_FILES[]

Further clarification access the following: https://secure.php.net/manual/en/features.file-upload.post-method.php

  • Thanks, I didn’t know I needed to use $_FILES[], I thought it was for $_POST[] That was the mistake, all the other modifications I had already made, only I was trying to access with $_POST[].

  • thank you very much, thank you.

  • OK.then mark as answered.

Browser other questions tagged

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