加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 注意:未定义索引

发布时间:2020-12-13 21:36:06 所属栏目:PHP教程 来源:网络整理
导读:参见英文答案 “Notice: Undefined variable”,“Notice: Undefined index”,and “Notice: Undefined offset” using PHP????????????????????????????????????27个 我有一个可以设置密码保护的复选框 – pstronglabel for="password"Password protect?/lab
参见英文答案 > “Notice: Undefined variable”,“Notice: Undefined index”,and “Notice: Undefined offset” using PHP????????????????????????????????????27个
我有一个可以设置密码保护的复选框 –

<p><strong><label for="password">Password protect?</label></strong> <input type="checkbox" name="password" id="password" value="1" /></p>

我的Php试图发布

$password = htmlspecialchars(strip_tags($_POST['password']));

我得到未定义的索引错误.

现在,如果我首先尝试检查密码是否已设置,我会得到相同的错误 –

$sql = "INSERT INTO blog (timestamp,title,entry,password) VALUES ('$timestamp','$title','$entry','$password')";

$result = mysql_query($sql) or print("Can't insert into table blog.<br />" . $sql . "<br />" . mysql_error());

我如何解决它?我是否必须为标题文本框等所有字段执行此操作?

解决方法

为什么要剥离标签并转义布尔值?你可以这样做:

$password = (isset($_POST['password']) && $_POST['password'] == 1) ? 1 : 0;

要么:

$password = isset($_POST['password']) ? (bool) $_POST['password'] : 0;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读