mardi 25 août 2015

PHP/MySQL - Checkboxes into Database

I found a few topics here on StackOverflow and I read a bit about how to insert data from checkboxes into a MySQL database. I read about using an array to insert the values into the database. Problem is now, that all data from my form are included as soon as I press the submit button except the checkbox values. So the form works as it should, only the checkbox values are not inserted into my database. Here is what I did:

A part of my form:

<form method="post" action="add_schulen.php" class="form-horizontal form-label-left">

<div class="form-group">
    <label class="control-label col-md-3 col-sm-3 col-xs-12">Name Schule:  </label>
<div class="col-md-9 col-sm-9 col-xs-12">
    <input type="text" name="schulname" class="form-control" placeholder="Namen der Schule">
</div>
</div>

    <div class="col-md-3 col-sm-3 col-xs-12 profile_left">
         <input type="checkbox" name="schulen[]" value="1A"> 1A <br />
         <input type="checkbox" name="schulen[]" value="1B"> 1B <br />
         <input type="checkbox" name="schulen[]" value="1C"> 1C <br /> 
         <input type="checkbox" name="schulen[]" value="1D"> 1D <br />
         <input type="checkbox" name="schulen[]" value="1E"> 1E <br />
         <input type="checkbox" name="schulen[]" value="1F"> 1F <br />
         <input type="checkbox" name="schulen[]" value="1G"> 1G <br />
         <input type="checkbox" name="schulen[]" value="1H"> 1H <br />
    </div>
<div class="col-md-3 col-sm-3 col-xs-12 profile_left">

         <input type="checkbox" name="schulen[]" value="2A"> 2A <br />
         <input type="checkbox" name="schulen[]" value="2B"> 2B <br />
         <input type="checkbox" name="schulen[]" value="2C"> 2C <br /> 
         <input type="checkbox" name="schulen[]" value="2D"> 2D <br />
         <input type="checkbox" name="schulen[]" value="2E"> 2E <br />
         <input type="checkbox" name="schulen[]" value="2F"> 2F <br />
         <input type="checkbox" name="schulen[]" value="2G"> 2G <br />
         <input type="checkbox" name="schulen[]" value="2H"> 2H <br />
</div>
    <button type="submit" name="addschulen" value="addschulen" class="btn btn-warning btn-lg pull-right">Schule anlegen</button>
</form>

My complete PHP code at the top of my add_schulen.php:

<?php 
include 'inc/database.php';    

// Check if form is submitted
if (isset ($_POST['addschulen'])) {
    $schulname = mysqli_real_escape_string ($connect, $_POST['schulname']);
    $ansprechperson = mysqli_real_escape_string ($connect, $_POST['ansprechperson']);
    $schulstrasse = mysqli_real_escape_string ($connect, $_POST['schulstrasse']);
    $schulplz = mysqli_real_escape_string ($connect, $_POST['schulplz']);
    $schulort = mysqli_real_escape_string ($connect, $_POST['schulort']);
    $schultelefon = mysqli_real_escape_string ($connect, $_POST['schultelefon']);
    $klassen = mysqli_real_escape_string ($connect, $_POST['schulen']);

        // Setting up a blank variable to be used in the coming loop.
      $alleKlassen = "";

      // For every checkbox value sent to the form.
      foreach ($klassen as $klasse) {
        // Append the string with the current array element, and then add a comma and a space at the end.
        $alleKlassen .= $klasse . ", ";
      }

      $query_insert_schule = mysqli_query($connect,"INSERT INTO `schule` (`schulname`, `ansprechperson`, `schulstrasse`, `schulplz`, `schulort`, `schultelefon`, `klasse`)
            VALUES ('$schulname', '$ansprechperson', '$schulstrasse', '$schulplz', '$schulort', '$schultelefon', '$alleKlassen')"); 

if (mysqli_affected_rows($connect) == 0) //<--
{
    die('Could not update data: ' . mysql_error());
} else {
    $msg_success= '<strong>Gratulation!</strong> Die Schule wurde erfolgreich hinzugefügt. Zur <a href="schulverwaltung.php"><span style="color:#fff;">Übersicht aller Schulen >></span></a>';
}       

}        
?>

As I said all data are included as soon as I click on the submit button, except the checkbox values. Does anyone have an idea why? What I am doing wrong? This is the first time I try to insert checkbox values into my database.

Thanks, Chris




Aucun commentaire:

Enregistrer un commentaire