Question

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.

Was it helpful?

Solution 2

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']) : '';

OTHER TIPS

An undefined index message means you are trying to access an array index that doesn't exist.
When using checkboxes, you don't get a value set if it's not checked.

So, when you process the form, check to see if the checkbox has been sent in.

$checkbox_value = (isset($checkbox_value)) ? $checkbox_value : 'default value';

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

You simply have to check if an array element is set before you use it, to avoid that warning.

$adds['WBR'] = $conn->real_escape_string($_POST['WBR']);

can be

if(isset($_POST['WBR']))
{
    $adds['WBR'] = $conn->real_escape_string($_POST['WBR']);
}

or

    $adds['WBR'] = isset($_POST['WBR']) ? $conn->real_escape_string($_POST['WBR']):0;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top