unlicked / checked单选按钮仍然输入0到db

时间:2015-03-24 13:58:00

标签: php html mysql

我正在处理这个巨大的表格,我有很多不同的输入,很多可以在空的时候发布。当发布表单并且未选中单选按钮组时,它仍然会在数据库中输入0。

db字段是tinyint,它允许null,这也是默认值。



<input name="participant" type="radio" id="partaker" class="with-gap" value="1"/>
<label for="partaker">Deelnemer</label>
<input name="participant" type="radio" id="participant_wait" class="with-gap" value="0"/>
<label for="participant_wait">Wachtlijst</label>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

如果使用POST,请尝试此操作(否则,将POST更改为GET):

if(isset($_POST['participant']) && empty($_POST['participant']))
    $_POST['participant'] = NULL;

替代解决方案,使用1,2作为单选按钮

if(isset($_POST['participant']) && $_POST['participant'] != 1 && $_POST['participant'] != 2)
     $_POST['participant'] = NULL;

如果您希望值为0和1,则只需添加else语句:

else {
   --$_POST['participant'];
}