Register selected checkbox values with php and Pdo

Asked

Viewed 115 times

0

I’m not being able to include the selected courses also would like to know how to register them separated by a "-"

        <div class="form-group">
<label for="checkbox" class="col-sm-3 control-label">Cursos</label>
<div class="col-sm-8">
<hr>
<div class="checkbox-inline1"><label><input type="checkbox" name="cursos[]" value="CCC"> CCC</label></div>
<div class="checkbox-inline1"><label><input type="checkbox" name="cursos[]" value="CTL"> CTL</label></div>
<div class="checkbox-inline1"><label><input type="checkbox" name="cursos[]" value="CLIB"> CLIB</label></div>
<div class="checkbox-inline1"><label><input type="checkbox" name="cursos[]" value="SILCI"> SILCI</label></div>
<div class="checkbox-inline1"><label><input type="checkbox" name="cursos[]" value="EIFOL"> EIFOL</label></div>
</div>
</div>

        if ($acao == 'incluir'):

        $isoDate = dateConvert($data_nascimento);

        $nome_foto = 'padrao.jpg';
        if(isset($_FILES['foto']) && $_FILES['foto']['size'] > 0):  

            $extensoes_aceitas = array('bmp' ,'png', 'svg', 'jpeg', 'jpg');
            $extensao = strtolower(end(explode('.', $_FILES['foto']['name'])));

             // Validamos se a extensão do arquivo é aceita
            if (array_search($extensao, $extensoes_aceitas) === false):
               echo "<h1>Extensão Inválida!</h1>";
               exit;
            endif;

             // Verifica se o upload foi enviado via POST   
             if(is_uploaded_file($_FILES['foto']['tmp_name'])):  

                  // Verifica se o diretório de destino existe, senão existir cria o diretório  
                  if(!file_exists("fotos")):  
                       mkdir("fotos");  
                  endif;  

                  // Monta o caminho de destino com o nome do arquivo  
                  $nome_foto = date('dmY') . '_' . $_FILES['foto']['name'];  

                  // Essa função move_uploaded_file() copia e verifica se o arquivo enviado foi copiado com sucesso para o destino  
                  if (!move_uploaded_file($_FILES['foto']['tmp_name'], '../fotos/'.$nome_foto)):  
                       echo "Houve um erro ao gravar arquivo na pasta de destino!";  
                  endif;  
             endif;  
        endif;

        $sql = 'INSERT INTO membros (membro, nome, data_nascimento, profissao, telefone, endereco, email, lider, batismo, ministerio, cursos, comentarios, foto)
                VALUES(:membro, :nome, :data_nascimento, :profissao, :telefone, :endereco, :email, :lider, :batismo, :ministerio, :cursos, :comentarios, :foto)';

        $stm = $conexao->prepare($sql);
        $stm->bindValue(':membro', $membro);
        $stm->bindValue(':nome', $nome);
        $stm->bindValue(':data_nascimento', $isoDate);
        $stm->bindValue(':profissao', $profissao);
        $stm->bindValue(':telefone', $telefone);
        $stm->bindValue(':endereco', $endereco);
        $stm->bindValue(':email', $email);
        $stm->bindValue(':lider', $lider);
        $stm->bindValue(':batismo', $batismo);
        $stm->bindValue(':ministerio', $ministerio);
        $stm->bindValue(':cursos', $cursos);
        $stm->bindValue(':comentarios', $comentarios);
        $stm->bindValue(':foto', $nome_foto);
        $retorno = $stm->execute();

        if ($retorno):
            echo "<div class='alert alert-success' role='alert'>Registro inserido com sucesso, aguarde você está sendo redirecionado ...</div> ";
        else:
            echo "<div class='alert alert-danger' role='alert'>Erro ao inserir registro!</div> ";
        endif;

        echo "<meta http-equiv=refresh content='3;URL=index.php'>";
    endif;

1 answer

1


By code, you are not handling the various courses which in case is an array.

In this case, you must transform the array into a string if the courses column is of this type. For this you can use the function implode php.

Below is an example:

$strcursos = implode(" - ", $cursos); 
echo $strcursos;

Browser other questions tagged

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