Undefined indices when saving the form in the database

advertisements

I'm having a little trouble with a form i'm saving in a database.

Basically, I have a form with checkboxes, every checkbox is connected to a column inside a mySQL database, when checked, and submitbutton is pressed, it saves the value from that checkbox, if not, it will stay empty.

Everyting works fine and saves to the database, but the boxes that are unchecked give the following notice:

Undefined index: WBR in /var/www/vhosts/leadserver.be/httpdocs/LPRamen/insert.php on line 35

here's a piece of the HTML:

<table>
     <tr>
         <td><input id="vlaams-brabant" type="checkbox" name="VBR" value="Vlaams-Brabant"/> Vlaams-Brabant</td>
         <td><input id="waals-brabant" type="checkbox" name="WBR" value="Waals-Brabant"/> Waals-Brabant </td>
     </tr>
     <tr>
         <td><input id="oost-vlaanderen" type="checkbox" name="OVL" value="Oost-Vlaanderen"/> Oost-Vlaanderen </td>
         <td><input id="west-vlaanderen" type="checkbox" name="WVL" value="West-Vlaanderen"/> West-Vlaanderen </td>
     </tr>

And so on and so on. The PHP Behind this is the following:

  $adds['nameCom'] = $conn->real_escape_string($_POST['nameCom']);
    $adds['name'] = $conn->real_escape_string($_POST['name']);
    $adds['number'] = $conn->real_escape_string($_POST['number']);
    $adds['email'] = $conn->real_escape_string($_POST['email']);

    $adds['VBR'] = $conn->real_escape_string($_POST['VBR']);
    $adds['WBR'] = $conn->real_escape_string($_POST['WBR']);
    $adds['OVL'] = $conn->real_escape_string($_POST['OVL']);
    $adds['WVL'] = $conn->real_escape_string($_POST['WVL']);
    $adds['LIM'] = $conn->real_escape_string($_POST['LIM']);
    $adds['ANT'] = $conn->real_escape_string($_POST['ANT']);
    $adds['LUI'] = $conn->real_escape_string($_POST['LUI']);
    $adds['HEN'] = $conn->real_escape_string($_POST['HEN']);
    $adds['LUX'] = $conn->real_escape_string($_POST['LUX']);
    $adds['NAM'] = $conn->real_escape_string($_POST['NAM']);
    $adds['NAT'] = $conn->real_escape_string($_POST['NAT']);
    $adds['INT'] = $conn->real_escape_string($_POST['INT']);
    $adds['BHG'] = $conn->real_escape_string($_POST['BHG']);

    $adds['leads'] = $conn->real_escape_string($_POST['leads']);
    $adds['akkoord'] = $conn->real_escape_string($_POST['akkoord']);
    //$adds['Regio'] = $conn->real_escape_string($_POST['Regio']);
    //Regio is geen string maar een array dus kan ook niet zo gedeclareerd worden
    /*$Regio = array();
    $adds['Regio'] = "";

    if(count($_POST['Regio']) > 0) {
        foreach($_POST['Regio'] as $key=>$value)
            $Regio[] = $conn->real_escape_string($value);
    }

    $adds['Regio'] = implode(',', $Regio);
    */
    // query voor INSERT INTO
    $sql = "INSERT INTO `dataramen`
    (`nameCom`, `name`, `number`, `email`, `VBR`, `WBR`, `OVL`, `WVL`, `LIM`, `ANT`, `LUI`, `HEN`, `LUX`, `NAM`, `NAT`, `INT`, `BHG`, `leads`, `akkoord`)
    VALUES
    ('". $adds['nameCom']. "', '". $adds['name']. "', '". $adds['number']. "', '". $adds['email']. "', '". $adds['VBR']. "', '". $adds['WBR']. "', '". $adds['OVL']. "', '". $adds['WVL']. "', '". $adds['LIM']. "', '". $adds['ANT']. "', '". $adds['LUI']. "', '". $adds['HEN']. "', '". $adds['LUX']. "', '". $adds['NAM']. "', '". $adds['NAT']. "', '". $adds['INT']. "', '". $adds['BHG']. "', '". $adds['leads']. "' , '". $adds['akkoord']. "')";

Keep in mind, These are just snippets of my code, the total is way to long to post

I know that there are several topics on this already but i've done the research and none of the solutions seem to work. If somebody could take a look at this and find out what's wrong, i would be most greatful.

I've also tried to set a standard value in the database if a field stays empty, that didn't work. I've set the indexes on the database columns correctly as well, but i still get the same notice.


Checkbox data will only be sent via $_POST if it's checked, otherwise the VBR key won't exist in your array.

You should check for it using the php function, isset().

$adds['VBR'] = (isset($_POST['VBR'])) ? $conn->real_escape_string($_POST['VBR']) : '';