There I come to you for help novalmente! I created a function to register images in the database, one of them being the cover and the rest of the album images.
The script even registers the cover image and album, but the album is only registered in the 1 (one) image bank, the most curious thing is that the files are moved to the uploads/
folder but are not registered in the database .
HTML:
<?php //Chama a função inserir caso tenha algum dado em POST (includes/conexao)
if (isset($_POST['salvar'])) {
criar_album('albumImagens', 'capa', $_POST);
}
?>
<form class="form-horizontal" method="POST" action="" enctype="multipart/form-data" runat="server" />
<fieldset>
<legend>Nova imagem</legend>
<div class="form-group">
<label class="col-md-3 control-label" for="nome">Nome</label>
<div class="col-md-6">
<input name="nome" type="text" placeholder class="form-control input-md" required />
</div>
</div>
<!-- Text input -->
<div class="form-group">
<div id="thumbnail">
</div>
<label class="col-md-3 control-label" for="capa">Capa:</label>
<div class="col-md-4">
<input type="file" name="capa" id="capa" class="form-control" required>
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-3 control-label" for="imagem">Album:</label>
<div class="col-md-4">
<input type="file" name="imagem[]" id="imagem" class="form-control" required multiple />
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-3 control-label" for="salvar"></label>
<div class="col-md-5">
<button type="submit" id="salvar" name="salvar" class="btn btn-primary btn-lg">Salvar</button>
</div>
</div>
</fieldset>
</form>
A function:
function criar_album($album, $destaque, $dados){
$con = conectar();
$caminho = 'uploads/';
$nome = $_POST['nome'];
$qtd = count($_FILES["imagem"]["name"]);
if ($qtd > 0){
$nomeArquivoCapa = $_FILES["capa"]["name"];
$nomeTemporarioCapa = $_FILES["capa"]["tmp_name"];
$tamanhoArquivoCapa = $_FILES["capa"]["size"];
for ($i=0; $i <= $qtd; $i++) {
$nomeArquivo = $_FILES["imagem"]["name"][$i];
$nomeTemporario = $_FILES["imagem"]["tmp_name"][$i];
$tamanhoArquivo = $_FILES["imagem"]["size"][$i];
if (!empty($nomeArquivoCapa)) {
$arquivoArrayCapa = explode(".", $nomeArquivoCapa);
$extensaoCapa = end($arquivoArrayCapa);
$arquivoCapa = $caminho.md5(time().rand(3212, 15452)).'.'.$extensaoCapa;
if (move_uploaded_file($nomeTemporarioCapa, $arquivoCapa)) {
$inserir = $con->prepare("INSERT INTO $destaque(nome, imagem) VALUES('$nome', '$arquivoCapa')");
$inseri = $inserir->execute(); // Execute a inserção
$last = $con->lastInsertId();
}
if (!empty($nomeArquivo)) {
$arquivoArray = explode(".", $nomeArquivo);
$extensao = end($arquivoArray);
$arquivo = $caminho.md5(time().rand(3212, 15452)).'.'.$extensao;
if (move_uploaded_file($nomeTemporario, $arquivo)) {
$inserir = $con->prepare("INSERT INTO $album(idCapa, imagem) VALUES('$last', '$arquivo')");
$inseri = $inserir->execute(); // Execute a inserção
}
else {
echo '<div class="alert alert-danger" role="alert">Erro, tente novamente mais tarde!</div>';
}
}
}
}
echo '<div class="alert alert-success" role="alert">Salvo com sucesso!</div>';
}
}
SQL Structure:
Table capa
:
ID, Name and Image
Table albumImagens
:
idCap, image
What seems to happen is that the for is not working, because it only makes one pass and nothing else. The% This is moving all the images at once in my view ...
I executed
if (move_uploaded_file($nomeTemporario, $arquivo))
to see ifvar_dump($inserir)
is being generated and I noticed that they are being generated YES but execute onlyquerys
happens by inserting only 1 image in the database.
object(PDOStatement)#3 (1) { ["queryString"]=> string(101) "INSERT INTO albumImagens(idCapa, imagem) VALUES('59', 'uploads/16487b1c9236bca1231deafb2e711d15.png')" } object(PDOStatement)#1 (1) { ["queryString"]=> string(101) "INSERT INTO albumImagens(idCapa, imagem) VALUES('59', 'uploads/7cb40a35907252ef2e0c401bc0561b1c.png')" }
Only image 1 with end UMA VEZ
is being inserted ...