-1

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.

j08691
  • 204,283
  • 31
  • 260
  • 272
CodeSigns
  • 53
  • 1
  • 2
  • 10
  • this is far too specific to your current problem for stackoverflow, and is essentially asking SO to debug your code. Please consider reducing your problem to a minimal test case that shows only the problematic behaviour and nothing else. I'm voting to close as too localised for now. – brice Mar 30 '13 at 02:17
  • I only wanted to solve this problem because I could not find how to fix it, after doing the research. I'm new to stackoverflow, is it not allowed to post a topic that specific? I meant no offense by this question, and I’m glad there were people available to help me. – CodeSigns Mar 30 '13 at 03:41
  • Don't worry, it just came up for review as being a bit 'Too localized' and I wanted you to know why we were voting this way. I'm glad you found an answer and made it work :) It is encouraged to ask specific questions, but questions that are only applicable to your particular problem should be generalised to be useful to everyone. I could see this was a genuine question, but it just didn't quite fit the Stackoverflow format. Have a read of the [FAQ](http://stackoverflow.com/faq#close) to see what I mean. – brice Mar 30 '13 at 03:57
  • If you generalise your question and think it should be reopened, don't hesitate to flag it up for reopening. Sadly, I can't help you pare the PHP down as I don't work with the language. – brice Mar 30 '13 at 03:59
  • Okay, thank you for the info brice. I'll be sure to keep that in mind! – CodeSigns Apr 02 '13 at 06:54

3 Answers3

2

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';
Trenton Trama
  • 4,890
  • 1
  • 22
  • 27
  • +1 for describing error AND checkboxes don't send when unchecked. Emphasism on the fact checkboxes **DO NOT** post back when unchecked. – UnholyRanger Mar 29 '13 at 16:04
1

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;
Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
1

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']) : '';
Adrian
  • 1,046
  • 7
  • 12