2
Here I come to ask for your help again! I created a function to register images in the database being one of them the cover and the rest of album images.
The script up to register the cover image and the album, but the album is only registered in the database 1 (an) image, the most curious thing is that the files are moved to the folder uploads/
but are not registered in the bank.
THE 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
:
idCapa, image
What seems to happen is that the is not working, because it only makes a passage and nothing else.
The excerpt if (move_uploaded_file($nomeTemporario, $arquivo))
This moving all images at once to my view...
I executed a
var_dump($inserir)
to see if thequerys
are being generated and noticed that SIM are being generated but run only happensUMA VEZ
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 ending 11d15.jpg
this being inserted...
Unfortunately still the same problem, only
uma imagem é cadastrada
at the bank.– RFL
@Rafaelacioly try to perform a debug to see the SQL being made, and after checking if it is saving with
idCapa
correspondent.– Guilherme Lautert
yes William, I put the code to force error display and what returned was
Notice: Undefined offset: 2
this error correlates the line just below thefor
, concerning the insertion of theidCapa
the same usually happens.– RFL
The reason for the
undefined
is that, I didn’t notice either, as if usingcount
it returns in the total number of records. and the array starts at 0, so the error is infor
which should be$i < $qtd
.– Guilherme Lautert
not yet solved, the problem is at the time of inserting the image in the database because the script is moving all at once and so does not return to the
for
– RFL
William, I made a
var_dump($inserir)
(after the query) and the return I had was2 querys para inserção
ie, querys are created but when executing it only executes a...– RFL
These two query are in the same string? if yes, check if they are separated by
;
, because one must be independent of the other.– Guilherme Lautert
Guilherme, I put the result of the query in the post, see what appears! it seems that there is no
;
– RFL
@Rafaelacioly it is performing a two separate moments, in this way no need of the
;
, but I can’t think of anything else that might be the problem. A tip, comment each part of the code, explaining, who knows how you find the error.– Guilherme Lautert