PHP表单复选框和未定义的索引 [英] PHP form checkbox and undefined index

查看:51
本文介绍了PHP表单复选框和未定义的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提交带有未选中复选框的表单时,出现未定义索引"错误.除了对每个单独的过帐值执行"isset"或"empty"检查外,还有其他方法吗?

I am getting an "Undefined index" error when submitting a form with an un-checked checkbox. Is there any other way besides running an "isset" or "empty" check on each individual posted value?

我已经看过这个问题,并且难以相信这是唯一的解决方案.

I have looked at this Question and am having trouble believing that this is the only solution.

下面是一些示例代码: 请不要这些不是表列的实际名称;它们具有唯一的名称(例如"postAddress,displayPhone,student等")

Below is some example code: please not that these are not the actual names of the tables columns; they are named uniquely (like "postAddress, displayPhone, student, etc.)

推荐答案

您可以编写一个函数来检查是否选中了一个复选框:

You could write a function that checks whether a checkbox was checked:

function checkbox_value($name) {
    return (isset($_POST[$name]) ? 1 : 0);
}

现在像这样在查询中调用该函数:

Now call that function in your query like this:

$sql =  'UPDATE table SET '.
        'checkbox1 = '. checkbox_value('checkbox1') .','.
        'checkbox2 = '. checkbox_value('checkbox2') .','.
        'checkbox3 = '. checkbox_value('checkbox3') .','.
        'checkbox4 = '. checkbox_value('checkbox4') .','.
        'checkbox5 = '. checkbox_value('checkbox5') .','. "LIMIT 1";

这篇关于PHP表单复选框和未定义的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆