How to save data from a checkbox to a MYSQL field

Asked

Viewed 867 times

1

Good night.

I have some checkbox fields, I want to save the selected data in only 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>";

?>

On the bench, saved array. I’m having trouble adapting the foreach or implode to save in the bank.

I need help!

1 answer

2


Use : implode("delimitador" , array[]); this function transforms the array $_POST['chkrepertorio']) in a string.

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

Would look like this:

$repertorio  =  addslashes(implode( "," , $_POST['chkrepertorio']));

Browser other questions tagged

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