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 theenctype
.– RFL
Amigo...the image you will access from $FILES
– Israel Zebulon
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.
– Ademir Colares
solved, thanks for the help! the files must be accessed by $_FILE[].
– Ademir Colares