0
I’ve been trying to post an image along with a text for a long time and I can’t. when I click on the sign up button, only enter the text, but the image does not register. see my code:
Insert file:
<?php
$conect = mysql_connect('mysql.hostinger.com.br','u728177807_post','cristaleye');
$db = mysql_select_db('u728177807_post');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Documento sem título</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
Título: <input type="text" name="titulo"/><br /><br />
<input type="file" name="imagem" value="imagem" />
Texto: <textarea name="texto" cols="30" rows="10"></textarea><br /><br />
<input type="hidden" name="acao" value="cad" />
<input type="submit" value="cadastrar" />
</form>
<?php
if(isset($_POST['acao']) && $_POST['acao'] == 'cad'){
$titulo = $_POST['titulo'];
$imagem = $_POST['imagem'];
$texto = $_POST['texto'];
if(empty($titulo) || empty($texto)){
echo '<script>alert("Preencha todos os campos!");</script>';
}else{
$inserir = mysql_query("INSERT INTO postagens (titulo, imagem, texto) VALUES ('$titulo','$imagem','$texto')");
echo '<script>alert("Cadastrado com sucesso!");</script>';
}}
?>
</body>
</html>
archive posts:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Documento sem título</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="corpo">
<?php
$seleciona = mysql_query("SELECT * FROM postagens ORDER BY id DESC");
$conta = mysql_num_rows($seleciona);
if($conta <= 0){
echo "Não há nenhum dado no banco!";
}else{
while($ln = mysql_fetch_array($seleciona)){
$titulo = $ln['titulo'];
$conteudo = $ln['texto'];
$imagem = $ln['imagem'];
?>
<div id="post">
<h1><?php echo $titulo; ?></h1>
<img src="images/<?php echo $imagem; ?>" width="450" height="280" border="0" />
<p><?php echo $conteudo; ?></p>
</div><br /><br />
<?php }} ?>
</div>
</body>
</html>
type the text and place the image in the insert.php but only enter the text in the posts.php
Start by reading the documentation Upload files with the POST method and look here on the site for "php image upload" which is several questions related to this. Probably this way you will already be able to do something else.
– Woss