We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 40092
    • 265 Posts
    Hi Guys,

    I'm getting error: attempt to set not null field anyfield to null

    when i am trying to save a number of fields out of a form.

    Putting it in an array like:

    'field' => 			$_POST['field'],
    
    


    This a checkbox that has a value of '1' (wich is the selected criteria).

    $selected="";
    if($field == "1"){
    $selected= "checked";
    }
    $outputted .= '<input type="checkbox" name="field"'.$selected.' value="1"></td>


    What is going wrong, any idea?

    Cheers,

    Robin

    This question has been answered by BobRay. See the first response.

    [ed. note: thapriest86 last edited this post 13 years, 1 month ago.]
    • discuss.answer
      • 3749
      • 24,544 Posts
      The problem is that fields that don't exist in the $_POST get set to null and the DB won't allow that value when you save.

      Try something like this:

      'field' => isset($_POST['field']) && (!empty($_POST['field'])) ? $_POST['field : '0',
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 40092
        • 265 Posts
        Ah, that's logical, indeed. Thanks.