How to write data from a checkbox in a MYSQL field [duplicate]

0

Good evening.

I have some checkbox fields, I want to save the selected data in just one field in the Database.

Part of the form:

<div class="form-group">
              <label class="col-md-4 control-label" for="chkrepertorio">Quais tipos de Música o Coral tem no repertório?</label>
              <div class="col-md-4">
              <div class="checkbox">
                <label for="chkrepertorio-0">
                  <input type="checkbox" name="chkrepertorio[]" id="chkrepertorio-0" value="Popular">
                  Popular
                </label>
                    </div>
              <div class="checkbox">
                <label for="chkrepertorio-1">
                  <input type="checkbox" name="chkrepertorio[]" id="chkrepertorio-1" value="Sacro">
                  Sacro
                </label>
                    </div>
              <div class="checkbox">
                <label for="chkrepertorio-2">
                  <input type="checkbox" name="chkrepertorio[]" id="chkrepertorio-2" value="Espiritualista">
                  Espiritualista
                </label>
                    </div>

File to save to bank:

    <?php

require 'conexao.php';

$nomedocoral                =           addslashes ($_POST['txtnomecoral']);
$enderecocoral              =           addslashes ($_POST['txtendereco']);
$emailcoral                 =           addslashes ($_POST['txtemail']);
$responsavelcoral           =           addslashes ($_POST['txtresponsavelcoral']);
$celularresponsavel         =           addslashes ($_POST['txtcelularresponsavel']);
$nomeregente                =           addslashes ($_POST['txtnomeregente']);
$celularregente             =           addslashes ($_POST['txtcelregente']);
$totalcoralistas            =           addslashes ($_POST['txttotalcoralistas']);
$totalmusicos               =           addslashes ($_POST['txttotalmusicos']);
$totalacompanhantes         =           addslashes ($_POST['txttotalacompanhantes']);
$totalgeral                 =           addslashes ($_POST['txttotalgeral']);
$repertorio                 =           addslashes ($_POST['chkrepertorio']);
$instrucoes                 =           addslashes ($_POST['Instruções']);

$sql = "INSERT INTO cantaguas18 SET nomedocoral = :nomedocoral, enderecocoral = :enderecocoral, emailcoral = :emailcoral, responsavelcoral = :responsavelcoral, celularresponsavel = :celularresponsavel, nomeregente = :nomeregente, celularregente = :celularregente, totalcoralistas = :totalcoralistas, totalmusicos = :totalmusicos, totalacompanhantes = :totalacompanhantes, totalgeral = :totalgeral, repertorio = :repertorio, instrucoes = :instrucoes";

$stmt = $PDO->prepare( $sql );
$stmt->bindParam( ':nomedocoral', $nomedocoral );
$stmt->bindParam( ':enderecocoral', $enderecocoral );
$stmt->bindParam( ':emailcoral', $emailcoral );
$stmt->bindParam( ':responsavelcoral', $responsavelcoral );
$stmt->bindParam( ':celularresponsavel', $celularresponsavel );    
$stmt->bindParam( ':nomeregente', $nomeregente );
$stmt->bindParam( ':celularregente', $celularregente );
$stmt->bindParam( ':totalcoralistas', $totalcoralistas );
$stmt->bindParam( ':totalmusicos', $totalmusicos );
$stmt->bindParam( ':totalacompanhantes', $totalacompanhantes );
$stmt->bindParam( ':totalgeral', $totalgeral );
$stmt->bindParam( ':repertorio', $repertorio );
$stmt->bindParam( ':instrucoes', $instrucoes );
$result = $stmt->execute();

echo '<script type="text/javascript">alert("Inscrição realizada com sucesso!");</script>';
echo "<script>window.location = 'https://pag.ae/bhwV2Hc';</script>";

?>

In the bank, saves array . I can not adapt foreach or implode to save in the bank.

I need help!

    
asked by anonymous 05.04.2018 / 04:46

1 answer

1

Use: implode("delimitador" , array[]); This function turns the array $_POST['chkrepertorio']) into a string.

When the data ( chkrepertorio[] ) reaches the server side, we can capture them as an array.

It would look like this:

$repertorio  =  addslashes(implode( "," , $_POST['chkrepertorio']));
    
05.04.2018 / 04:58