Problem with the check boxes and adding value to the database

advertisements

I'm trying to add values of some checkboxes in my database but it's not really working. It gives "" values:

This is the query it creates: INSERT INTO oudebadkamer(WC, Bad, Douche, Wasbak) VALUES ( '','','','');

The connection is ok and the names are correct since it adds "" in all the fields.

Please help me out.

 <form action="formhandle.php" method="POST">

    Wat zit er in uw huidige badkamer?<br />
    <input type="checkbox" name="Sanitair[]" value="WC" checked>WC<br />
    <input type="checkbox" name="Sanitair[]" value="Douche" checked>Douche<br />
    <input type="checkbox" name="Sanitair[]" value="Bad" checked>Bad<br />
    <input type="checkbox" name="Sanitair[]" value="Wasbak" checked>Wasbak<br />    

    <input type="submit" name="Volgende" value="Volgende"/><br />
    <?php
    $checkbox1 = $_POST['Sanitair'];
    if(isset ($_POST ['Volgende']))
    {
            $querystring = "INSERT INTO oudebadkamer(WC, Bad, Douche, Wasbak)
                          VALUES (
                          '"
                          .$checkbox1[0]."','"
                          .$checkbox1[1]."','"
                          .$checkbox1[2]."','"
                          .$checkbox1[3]."');";
            mysql_query($querystring)or die(mysql_error());

            echo $querystring;
    }
    ?>
    </form>


  1. Close you form ie. </form> before starting to process PHP.

i have edited your code:

It will work now !

<form action="formhandle.php" method="POST">

Wat zit er in uw huidige badkamer?<br />
<input type="checkbox" name="Sanitair[]" value="WC" checked>WC<br />
<input type="checkbox" name="Sanitair[]" value="Douche" checked>Douche<br />
<input type="checkbox" name="Sanitair[]" value="Bad" checked>Bad<br />
<input type="checkbox" name="Sanitair[]" value="Wasbak" checked>Wasbak<br />    

<input type="submit" name="Volgende" value="Volgende"/><br />

</form>
<?php
if(isset ($_POST ['Volgende']))
{
    $val1 =$val2 = $val3 = $val4 = FALSE; // declaring all variables to be false

    if(isset($_POST['Sanitair'][0]))
    {
        $val1 = $_POST['Sanitair'][0]; // if value is set then put the value in variable else keep it empty
    }
    else
    {
       $val1 = '';
    }

    if(isset($_POST['Sanitair'][1]))
    {
        $val2 = $_POST['Sanitair'][1];
    }
    else
    {
       $val2 = '';
    }

    if(isset($_POST['Sanitair'][2]))
    {
        $val3 = $_POST['Sanitair'][2];
    }
    else
    {
       $val3 = '';
    }

    if(isset($_POST['Sanitair'][3]))
    {
        $val4 = $_POST['Sanitair'][3];
    }
    else
    {
       $val4 = '';
    }

    if($val1 or $val2 or $val3 or $val4)
    {
         $querystring = "INSERT INTO oudebadkamer (`WC`, `Bad`, `Douche`, `Wasbak`)
                      VALUES ( '".$val1."','".$val2."','".$val3."','".$val4."')";
        mysql_query($querystring)or die(mysql_error());
// use backtics with column name while inserting it will help reducing future  errors
        echo $querystring;
    }
    else
    {
        echo "no Checkbox selected";
    }
}
?>