Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Pessoal...to com um problemão... preciso gravar varias imgs no bd... é um site de uma imobiliaria com varias fotos de casas.. só q nao consigo gravar varias fotos apenas uma...
>
<?php
include"../conexao.php";
?>
<!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">](http://www.w3.org/1999/xhtml%22)
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
//Formulário
echo '<form method="post" action="'.$PHP_SELF.'" enctype="multipart/form-data">'
?>
<table width="100%" border="0" cellspacing="3" cellpadding="0">
<tr>
<td width="13%"><div align="right">Categoria:</div></td>
<td width="87%"><label>
<input type="text" name="categoria" id="categoria" />
</label></td>
</tr>
<tr>
<td><div align="right">Código:
</div></td>
<td><label>
<input type="text" name="codigo" id="codigo" />
</label></td>
</tr>
<tr>
<td><div align="right">Endereço:</div></td>
<td><label>
<input type="text" name="endereco" id="endereco" />
</label></td>
</tr>
<tr>
<td><div align="right">Cidade:</div></td>
<td><label>
<input type="text" name="cidade" id="cidade" />
</label></td>
</tr>
<tr>
<td><div align="right">Andares:</div></td>
<td><label>
<input type="text" name="andares" id="andares" />
</label></td>
</tr>
<tr>
<td><div align="right">Dormitórios:</div></td>
<td><label>
<input type="text" name="dormitorios" id="dormitorios" />
</label></td>
</tr>
<tr>
<td><div align="right">Suítes:</div></td>
<td><label>
<input type="text" name="suites" id="suites" />
</label></td>
</tr>
<tr>
<td><div align="right">Descrição:</div></td>
<td><label>
<input type="text" name="descricao" id="descricao" />
</label></td>
</tr>
<tr>
<td><div align="right">Preço:</div></td>
<td><label>
<input type="text" name="preco" id="preco" />
</label></td>
</tr>
<tr>
<td><div align="right">Fase da Obra:</div></td>
<td><label>
<input type="text" name="fase" id="fase" />
</label></td>
</tr>
<tr>
<td><div align="right">Tipo:</div></td>
<td><label>
<input type="text" name="tipo" id="tipo" />
</label></td>
</tr>
<tr>
<td><div align="right">Pagamento:</div></td>
<td><label>
<input type="text" name="pagamento" id="pagamento" />
</label></td>
</tr>
<tr>
<td><div align="right">Foto:</div></td>
<td><label>
<input type="file" name="file[]" id="foto1" />
</label></td>
</tr>
<tr>
<td><div align="right">Descrição da Foto:</div></td>
<td><label>
<input type="text" name="desc1" id="desc1" />
</label></td>
</tr>
<tr>
<td><div align="right">Foto:</div></td>
<td><label>
<input type="file" name="file[]" id="foto2" />
</label></td>
</tr>
<tr>
<td><div align="right">Descrição da Foto:</div></td>
<td><label>
<input type="text" name="desc2" id="desc2" />
</label></td>
</tr>
<tr>
<td><div align="right">Foto:</div></td>
<td><label>
<input type="file" name="file[]" id="foto3" />
</label></td>
</tr>
<tr>
<td><div align="right">Descrição da Foto:</div></td>
<td><label>
<input type="text" name="desc3" id="desc3" />
</label></td>
</tr>
<tr>
<td><div align="right">Foto:</div></td>
<td><label>
<input type="file" name="file[]" id="foto4" />
</label></td>
</tr>
<tr>
<td><div align="right">Descrição da Foto:</div></td>
<td><label>
<input type="text" name="desc4" id="desc4" />
</label></td>
</tr>
<tr>
<td><div align="right">Foto:</div></td>
<td><label>
<input type="file" name="file[]" id="foto5" />
</label></td>
</tr>
<tr>
<td><div align="right">Descrição da Foto:</div></td>
<td><label>
<input type="text" name="desc5" id="desc5" />
</label></td>
</tr>
</table>
<input type="submit" name="submit" value=" OK ">
</form>
<?php
//Diretório aonde ficará os arquivos
$dir = "../fotos/";
//Extensões permitidas
$ext = array("gif","jpg","png");
//Quant. de campos do tipo FILE
$campos = 6;
//Se for enviado
if (isset($_POST['submit'])) {
//Obtendo info. dos arquivos
$f_name = $_FILES['file']['name'];
$f_tmp = $_FILES['file']['tmp_name'];
$f_type = $_FILES['file']['type'];
//Contar arquivos enviados
$cont=0;
$caminho = "fotos/";
//Repetindo de acordo com a quantidade de campos FILE
for($i=0;$i<$campos;$i++){
//Pegando o nome
$name = $f_name[$i];
$arquivo[$i] = $caminho.$f_name[$i];
echo "$arquivo[1] <br> $arquivo[2]";
//Verificando se o campo contem arquivo
if ( ($name!="") and (is_file($f_tmp[$i])) and (in_array(substr($name, -3),$ext)) ) {
if ($cont==0) {
echo "<b>Arquivo(s) enviados:
</b>";
}
echo $name." - ";
$categoria = $_POST['categoria'];
$codigo = $_POST['codigo'];
$endereco = $_POST['endereco'];
$cidade = $_POST['cidade'];
$andares = $_POST['andares'];
$dormitorios = $_POST['dormitorios'];
$suites = $_POST['suites'];
$preco = $_POST['preco'];
$fase_obra = $_POST['fase_obra'];
$tipo = $_POST['tipo'];
$pagamento = $_POST['pagamento'];
$categoria = $_POST['categoria'];
$categoria = $_POST['categoria'];
$categoria = $_POST['categoria'];
$sql = "INSERT INTO imoveis ( categoria , codigo , endereco , cidade , andares, dormitorios, suites, descricao, preco, fase_obra, tipo, pagamento, foto1, desc1, foto2, desc2, foto3, desc3, foto4, desc4, foto5, desc5 ) VALUES ( '$categoria' , '$codigo' , '$endereco' , '$cidade' , '$andares' , '$dormitorios', '$suites', '$descricao', '$preco', '$fase', '$tipo', '$pagamento', '$arquivo[1]', '$desc1', '$arquivo[2]', '$desc2', '$arquivo[3]', '$desc3', '$arquivo[4]', '$desc4', '$arquivo[5]', '$desc5' )";
echo "$sql";
$rsExec = mysql_query($sql);
if(mysql_affected_rows()==1){
echo "<script type=\"text/javascript\">
confirma();
</script>";
echo "<meta HTTP-EQUIV='Refresh' CONTENT='0;URL=index.php'>";
}else{
//verifico se nao estao tentando gravar um dado que ja existe, pois usei UNIQUE na tabela
if(mysql_errno() == 1062) {
echo $erros[mysql_error()];
exit;
}else{
echo "Erro! Nao foi possivel efetuar o cadastro!!!";
exit();
}
mysql_close($conexao);
} //se o arquivo nao for inserido ele execura o else
//Movendo arquivo's do upload
$up = move_uploaded_file($f_tmp[$i], $dir.$name);
//Status
if ($up==true):
echo "<i>Enviado!</i>";
$cont++;
else:
echo "<i>Falhou!</i>";
endif;
echo "
";
}
}
echo ($cont!=0) ? "<i>Total de arquivos enviados: </i>".$cont : "Nenhum arquivo foi enviado!";
}
?>
</body>
</html>
BD
>
CREATE TABLE `imoveis` (
`id_imovel` int(11) NOT NULL,
`categoria` varchar(100) NOT NULL,
`codigo` varchar(10) NOT NULL,
`endereco` varchar(200) NOT NULL,
`cidade` varchar(100) NOT NULL,
`andares` varchar(50) NOT NULL,
`dormitorios` varchar(10) NOT NULL,
`suites` varchar(10) NOT NULL,
`descricao` varchar(300) NOT NULL,
`preco` varchar(50) NOT NULL,
`fase_obra` varchar(50) NOT NULL,
`tipo` varchar(50) NOT NULL,
`pagamento` varchar(50) NOT NULL,
`foto1` varchar(100) NOT NULL,
`desc1` varchar(50) NOT NULL,
`foto2` varchar(100) NOT NULL,
`desc2` varchar(50) NOT NULL,
`foto3` varchar(100) NOT NULL,
`desc3` varchar(50) NOT NULL,
`foto4` varchar(100) NOT NULL,
`desc4` varchar(50) NOT NULL,
`foto5` varchar(100) NOT NULL,
`desc5` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;Carregando comentários...