Jump to content

[PHP+MYSQL] Inserir dados BD


david_carlos
 Share

Recommended Posts

Boas pessoal,

estou aqui com uma duvida, então é o seguinte tenho uma pagina (projectos) que tem nome, marca, categoria, descrição e imagens.

Agora é o seguinte eu estou a fazer dois inserts 1 vai guardar o projecto até aqui tudo bem. Agora o outro guarda simplesmente os links das fotos e a projecto estão associadas.

não estou a conseguir fazer a parte de inserir outro projecto a apanhar o id correcto para associar as fotos vejam o código svf

<?php
include("mysql.php");
include("resize-class.php");
if (isset($_SESSION["tipo_user"]) == "admin" ){?>
<div align="center" id="inserir_projectos" >
<form action="" method="post" enctype="multipart/form-data">

<table width="435" border="1">
<tr>
<td height="42" colspan="2"><div align="center"><strong>Inserir Projecto </strong></div></td>
<td width="70" rowspan="6"> </td>
</tr>
<tr>
<td width="72">Nome:</td>
<td width="271"><label>
 <input name="txt_nome" type="text" id="txt_nome" size="30" />
 </label> </td>
</tr>
<tr>
<td>Categoria:</td>
<td><select name="cb_categorias">
 <option value="0">Selecionar Categoria</option>
<option value="1">Peliculas</option>
<option value="2">CarAudio</option>
<option value="3">Vinil</option>
</select>

</td>
</tr>
<tr>
<td>Marca:</td>
<td><select name="cb_marca" id="select">
 <?php $sql = mysql_query("select * from marcas ");
?>
<option>Selecionar Marca</option>
<?php
while($row = mysql_fetch_array($sql))
{

echo "<option name=\"cb_marca\" value='".$row['id_marca']."'>".$row['nome']."</option>";
}?>
 </select></td>
</tr>
<tr>
<td>Imagens:</td>
<td><input type="file" name="img[]" multiple></td>
</tr>
<tr>
<td>Descrição</td>
<td><textarea name="txt_nome2" cols="30" id="txt_nome2"></textarea></td>
</tr>
<tr>
<td colspan="2" rowspan="2"> </td>
<td><div align="center">
 <input name="upload" type="submit" value="Guardar" />
</div></td>
</tr>
<tr>
<td height="37"> </td>
</tr>
</table>
</form>
</div>
<?php
if(isset($_POST['upload'])){
$nome=$_POST['txt_nome'];
$categoria=$_POST['cb_categorias'];
$marca=$_POST['cb_marca'];
$descricao=$_POST['txt_nome2'];


//INFO IMAGEM
$file = $_FILES['img'];
$numFile = count(array_filter($file['name']));

//PASTA
$folder = 'img/fotos/';

//REQUISITOS
$permite = array('image/jpeg', 'image/png');
$maxSize = 1024 * 1024 * 5;

//MENSAGENS
$msg = array();
$errorMsg = array(
1 => 'O arquivo no upload é maior do que o limite definido em upload_max_filesize no php.ini.',
2 => 'O arquivo ultrapassa o limite de tamanho em MAX_FILE_SIZE que foi especificado no formulário HTML',
3 => 'o upload do arquivo foi feito parcialmente',
4 => 'Não foi feito o upload do arquivo'
);

if($numFile <= 0)
echo 'Selecione uma Imagem!';
else{
for($i = 0; $i < $numFile; $i++){
$name = $file['name'][$i];
$type = $file['type'][$i];
$size = $file['size'][$i];
$error = $file['error'][$i];
$tmp = $file['tmp_name'][$i];

$extensao = @end(explode('.', $name));
$novoNome = array();
$novoNome[] = rand().".$extensao";

if($error != 0)
 $msg[] = "<b>$name :</b> ".$errorMsg[$error];
else if(!in_array($type, $permite))
 $msg[] = "<b>$name :</b> Erro imagem não suportada!";
else if($size > $maxSize)
 $msg[] = "<b>$name :</b> Erro imagem ultrapassa o limite de 5MB";
else{

 #$move = move_uploaded_file($tmp, $folder.'/'.$novoNome);

 #$resizeObj = new resize($folder.'/'.$novoNome);
 #$resizeObj -> resizeImage(780, 780, 'auto');
 #$resizeObj -> saveImage($folder.'/'.$novoNome , 100);



$arrlength = count($novoNome);
for($x = 0; $x < $arrlength; $x++) {
 echo $novoNome[$x];
 echo "<br>";



}


#$vamos="Insert into fotos (id_fotos,id_projectos,foto)VALUES('','1','$novoNome')";
#$vai=mysql_query($vamos)or die("deu erro aqui fotos");

#$query = "SELECT LAST_INSERT_ID() INTO 'projectos' @id_projectos ";


}}}

#$sql="Insert into projectos (id_projectos,nome,descricao,categoria,id_marca)VALUES('','".$nome."','".$descricao."','".$categoria."','".$marca."')";
#$query=mysql_query($sql)or die("deu erro aqui projectos");



}
}
else{
echo "<strong><font color=\"#FF0000\" >Não é Administrador para ter acesso a esta página!!!</font></strong>";

}
?>
Edited by david_carlos
geshi
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.