Image upload saves only first image in database

Asked

Viewed 67 times

1

I have the following image upload code

if(isset($_POST['upload'])){

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

    //PASTA
    $folder     = 'upload';

    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];
            $tmp    = $file['tmp_name'][$i];
            $extensao = @end(explode('.', $name));
            $novoNome = rand().".$extensao";
            $identificador = $_POST['EdicaoId'];
            $UrlImagem = $folder.'/'.$novoNome;
                if(move_uploaded_file($tmp, $folder.'/'.$novoNome)){
                $SalvaUrl = $conn->prepare("INSERT INTO revistas (id,url) VALUES (:id,:url)");
                    $SalvaUrl->bindParam(':id',$identificador);
                    $SalvaUrl->bindParam(':url',$UrlImagem);
                    $SalvaUrl->execute();
        }}}}

Only images are saved in the upload directory, but when uploading to the database only the first one selected is registered.

1 answer

2


You’re using the same ID to insert all the images. I understand that’s what you want, to link all the images to the same record, but is your DDL prepared for that? Two Inserts for the same ID in this table will not violate some Constraint? That would explain the entry of a record, but no more.

  • thank you very much was this, the ID was set as the primary key.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.