vendredi 10 janvier 2020

Multiple checkboxes into database form

I have problem adding multiple checkboxes into database that already has other types of forms. When I submit my form only 1 checkbox is uploaded into my db, regardless of how much I have selected. There is my form code:

        <form action="mywebsite/#ankieta<--this works" class="needs-validation" novalidate="" method="post">
        <div class="row">
          <div class="col-md-6 mb-3">
            <label for="firstName">Jaki termin Ciebie najbardziej interesuje?</label>
            <div class="d-block my-3">
              <div class="custom-control custom-checkbox">
                <input id="data1" name="termin" type="checkbox" value="25.01.2020" class="custom-control-input" checked="" required="">
                <label class="custom-control-label" for="data1">25.01.2020</label>
              </div>
              <div class="custom-control custom-checkbox">
                <input id="data2" name="termin" type="checkbox" value="01.02.2020" class="custom-control-input" required="">
                <label class="custom-control-label" for="data2">01.02.2020</label>
              </div>
              <div class="custom-control custom-checkbox">
                <input id="data3" name="termin" type="checkbox" value="15.02.2020" class="custom-control-input" required="">
                <label class="custom-control-label" for="data3">15.02.2020</label>
              </div>
            </div>
          </div>
          <div class="col-md-6 mb-3">
            <label for="iloscOsob">Liczba zgłaszanych osób.</label>
            <div class="d-block my-3">
            <input class="form-control" type="number" name="liczbaosob" value="1" min="1" max="99" id="iloscOsob" required="required">
            </div>
          </div>
          <div class="col-md-6 mb-3">
            <label for="firstName">Czy interesuje Cię wypożyczenie sprzętu?</label>
            <div class="d-block my-3">
              <div class="custom-control custom-radio">
                <input id="sprzettak" name="sprzet" type="radio" value="tak" class="custom-control-input" checked="" required="">
                <label class="custom-control-label" for="sprzettak">Tak</label>
              </div>
              <div class="custom-control custom-radio">
                <input id="sprzetnie" name="sprzet" type="radio" value="nie" class="custom-control-input" required="">
                <label class="custom-control-label" for="sprzetnie">Nie</label>
              </div>
            </div>
          </div>
        </div>

        <hr class="mb-4">

        <div class="row">
          <div class="col-md-6 mb-3">
            <label for="sendname">Imię i nazwisko</label>
            <input name="imienazwisko" type="name" class="form-control" id="sendname" placeholder="Jan Kowalski">
            <div class="invalid-feedback">
              Please enter a valid email address for shipping updates.
            </div>
          </div>

          <div class="col-md-6 mb-3">
            <label for="sendemail">Email</label>
            <input name="email" type="email" class="form-control" id="sendemail" placeholder="moj@email.com">
            <div class="invalid-feedback">
              Please enter a valid email address for shipping updates.
            </div>
          </div>
        </div>

        <hr class="mb-4">
        <div class="mb3">
          <label for="same-address">Czy masz jakieś pytanie?</label>
          <textarea name="pytanie" class="form-control" id="same-address" placeholder="Max. 255 znaków"></textarea>
        </div>

        <hr class="mb-4">
        <button class="btn btn-primary btn-lg btn-block" type="submit" name="submit">Wyślij</button>
      </form>

And my PHP:

<?php
$imienazwisko = $_POST['imienazwisko'];
$termin = $_POST['termin'];
$liczbaosob = $_POST['liczbaosob'];
$sprzet = $_POST['sprzet'];
$email = $_POST['email'];
$pytanie = $_POST['pytanie'];

$conn = new mysqli('localhost', 'username', 'password', 'db');
if ($conn->connect_error){
header("location: index.php?signup=error&error=" . urlencode('Connection failed: ' . $conn->connect_error)); exit;
}
else{
$stmt = $conn->prepare("insert into form1(imienazwisko, termin, liczbaosob, sprzet, email, pytanie)
    values(?, ?, ?, ?, ?, ?)");
$stmt->bind_param("ssssss", $imienazwisko, $termin, $liczbaosob, $sprzet, $email, $pytanie);
$stmt->execute();
if ($stmt->error) {
    header("location: index.php?signup=error&error=" . urlencode($stmt->error));

    $stmt->close();
    $conn->close();
    exit;
} else {
    $stmt->close();
    $conn->close();
    header("location: index.php?signup=success");
    exit;
}
}

I know i somehow must add an array into termin (temin[]) but IDK how to do it in existing PHP file, I have looked for help here but was unable to apply anything I have found, unfortunatelly. BTW everything else works perfect, I changed code a little. Thank you in advance.




Aucun commentaire:

Enregistrer un commentaire