imgrooot Posted August 22, 2016 Share Posted August 22, 2016 I am finding that if I have "0"(zero) value in form select option, it won't select this option or submit data. If I change this value to any other number to text, it will work. Is there a way to fix this? I have to have an option where I am able to choose to submit "0" value to the database table. <option value="0" <?php if(empty($_POST['special'])) {} else { if($_POST['special'] == 0) { echo 'selected'; } } ?> >None</option> Quote Link to comment https://forums.phpfreaks.com/topic/301965-this-is-strange-it-wont-insert-0-option-value-into-table/ Share on other sites More sharing options...
Barand Posted August 22, 2016 Share Posted August 22, 2016 that's because zero is considered to be "empty" eg $a = 0; if (empty($a)) echo 'ok'; //--> ok Quote Link to comment https://forums.phpfreaks.com/topic/301965-this-is-strange-it-wont-insert-0-option-value-into-table/#findComment-1536440 Share on other sites More sharing options...
imgrooot Posted August 22, 2016 Author Share Posted August 22, 2016 that's because zero is considered to be "empty" eg $a = 0; if (empty($a)) echo 'ok'; //--> ok So what's the solution? The column I am inserting into is "int" based. So I need an int number to distinguish from all the other numbers. Is there no way to add zero to it? Quote Link to comment https://forums.phpfreaks.com/topic/301965-this-is-strange-it-wont-insert-0-option-value-into-table/#findComment-1536441 Share on other sites More sharing options...
imgrooot Posted August 22, 2016 Author Share Posted August 22, 2016 The only other solution I can think of is to turn the table into "varchar" and use a unique text like "none" to replace "0". Quote Link to comment https://forums.phpfreaks.com/topic/301965-this-is-strange-it-wont-insert-0-option-value-into-table/#findComment-1536442 Share on other sites More sharing options...
Barand Posted August 22, 2016 Share Posted August 22, 2016 All you have shown us is a single line of incorrect code and I pointed out what is wrong with that code. You could try <option value="0" <?php if ($_POST['special'] === '0') echo 'selected'; ?> >None</option> On the basis of a single line I can not and will not comment on an application solution, as I have no idea what the significance of the "0" versus "" is in the overall context. But your proposed alternative sounds wrong. Quote Link to comment https://forums.phpfreaks.com/topic/301965-this-is-strange-it-wont-insert-0-option-value-into-table/#findComment-1536443 Share on other sites More sharing options...
techboy992 Posted August 22, 2016 Share Posted August 22, 2016 Hi The only other solution I can think of is to turn the table into "varchar" and use a unique text like "none" to replace "0". You could direct set it to 0 in the database using the default option and choose as defined =0 if it's a empty value it will set it to 0 Quote Link to comment https://forums.phpfreaks.com/topic/301965-this-is-strange-it-wont-insert-0-option-value-into-table/#findComment-1536444 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.