PHP未定义的索引ID,但如果(isset($ _ POST ['ID']))语句正常工作 [英] php undefined index id but if(isset($_POST['id'])) statement working fine

查看:197
本文介绍了PHP未定义的索引ID,但如果(isset($ _ POST ['ID']))语句正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是php的新手,并且在每个论坛上都在阅读关于此问题的文章,但没有任何工作,但它仍然给我提示:未定义索引。任何帮助将不胜感激。

  if(isset($ _ POST ['redeem_points'])){

$ id = $ _POST [ 'ID'];
$ points = mysqli_real_escape_string($ conn,$ _ POST ['points']);
$ sql =更新用户设置user_points = user_points - '。$ points。''WHERE user_id ='。 $ id。';
$ result = mysqli_query($ conn,$ sql);
if($ result === TRUE){
echo'< script> window.alert(Points Redeemed successfully!)
location.href =admin_manage_points.php< / script>';
}
else {
echofailed;
}
}


解决方案

isset()放在 $ _ POST 数组元素上,以确保它们已被设置,例如:

  $ id = isset($ _ POST ['id'])? $ _POST ['id']:false; 

如果您使用PHP 7或更高版本,您可以使用以下内容:

  //返回$ _POST ['id'] 
的值//如果不存在,则返回false。
$ id = $ _POST ['id'] ??假;

空合并运算符(??)返回第一个操作数,如果它存在并且不为NULL;否则返回第二个操作数。

I am new to php and have been reading on every forum about this issue but nothing works it still gives me the warning: undefined index. Any help would be greatly appreciated.

if(isset($_POST['redeem_points'])){

            $id = $_POST['id'];
            $points = mysqli_real_escape_string($conn,$_POST['points']);
            $sql = "UPDATE users set user_points = user_points - '".$points."' WHERE user_id = '". $id ."' ";
            $result = mysqli_query($conn,$sql);
            if ($result === TRUE) {
            echo '<script> window.alert("Points Redeemed successfully !")
                        location.href = "admin_manage_points.php" </script>';
                }
                else{
                    echo "failed";
                    }
                } 

解决方案

use isset() on $_POST array elements, to make sure they are set, e.g.:

$id = isset($_POST['id']) ? $_POST['id'] : false;

If you use PHP 7 or higher you can use the following:

// returns the value of $_POST['id']
// or returns false if it does not exist.
$id = $_POST['id'] ?? false;

The null coalescing operator (??) returns the first operand if it exists and is not NULL; otherwise it returns its second operand.

这篇关于PHP未定义的索引ID,但如果(isset($ _ POST ['ID']))语句正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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