1

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

5
  • try $klassen = mysqli_real_escape_string ($connect, $_POST['schulen[]']); Commented Aug 25, 2015 at 11:31
  • Thank you for your help but still the same issue. Everything goes into my database except the checkbox values Commented Aug 25, 2015 at 11:41
  • What does var_dump($klassen); give (before any modifications)? Commented Aug 25, 2015 at 11:52
  • I entered var_dump($klassen); after my foreach function. It looks like this: 'foreach ($klassen as $klasse) { $alleKlassen .= $klasse . ", "; } var_dump($klassen);' and it shows me nothing as soon as I click on the submit button. Did I insereted the var_dump wrong? Commented Aug 25, 2015 at 11:54
  • @ChristophC. I think you misunderstood me. If you do var_dump($klassen); directly after the $_POSTs are being fetched, then run the form, what values does var_dump($klassen); output? Commented Aug 25, 2015 at 11:56

2 Answers 2

1

well i have tryied and it worked for just didnt insert into db but i just printed it out. try taking away the string escape from $_POST['schulen'] since that is already cleaned/ prefdefined data. and also use substr() to remove the last comma at the end of the array.

try this:

<?php
// Check if form is submitted
if (isset ($_POST['addschulen'])) {
    $schulname =  $_POST['schulname'];
    $ansprechperson =$_POST['ansprechperson'];
    $schulstrasse = $_POST['schulstrasse'];
    $schulplz =$_POST['schulplz'];
    $schulort = $_POST['schulort'];
    $schultelefon =$_POST['schultelefon'];
    $klassen = $_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 . ", ";
      }
    $alleKlassen  = substr($alleKlassen , 0, -2);
    print_r($alleKlassen);

}else{
    echo '<form method="post" action="" 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>';
}
?>
Sign up to request clarification or add additional context in comments.

3 Comments

AWESOME thank you so much! That´s it! It was because of the string escape! I did not know that :)! And one last thing: Can you also tell me what my code should look like, when I now want to output those checkboxes from the database into a dropdown list? So that on another page a user can select from a dropdown list every checkbox that I have included into my database?
ohkk i hope this helps, when u have a string in php like this $checkbox= "1A,2C,7N"; you can use the 'explode()' function to get them individually so you can put it in the dropdown . try this or check it out [here](codepad.org/t0z1W3Vg )
thank you! I looked at the codepad link and I will try it out. Thank you once again!
0

Since you are using html arrays you must foreach on them like an array:

$schulen= $_POST['schulen'];
foreach( $schulen as $s ){
    //code
}

now if you write: echo $s; (under //code) it will print the value of the checkbox that were checked by the user... so now it should be easy for you to solve ;) example, if you want to escape:

$schulen= $_POST['schulen'];
for( $i=0; $i< count($schulen); $i++ ){
    $schulen[ $i ]= mysqli_real_escape_string( $schulen[ $i ] );
}

if you want to put into database you need an apropriate filed (example: a text field will be good) then you must use implode & explode... to put the array: $todatabase= implode( $schulen, '|' ); to get the array: $schulen= explode( '|', $fromdatabase );

4 Comments

I wrote now: foreach ($klassen as $klasse) { echo $klasse; } and as soon as I submit my form, it is the same...everything is inserted into my database except the checkbox values
He doesn't have to use implode or explode, he's building a string from the array in the foreach.
var_dump($_POST) and look what you get from checkboxes! Try to add some of them manually to mysql!
$alleKlassen = ""; foreach ($schulen as $klasse) { $alleKlassen .= $klasse . ", "; } after my second example then should be fine!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.