php未定义索引ID,但if(isset($ _ POST ['id']))语句正常工作

问题描述 投票:-2回答:3

我是php的新手,并且已经在每个论坛上阅读有关此问题但没有任何作用它仍然给我警告:未定义的索引。任何帮助将不胜感激。

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";
                    }
                } 
php html mysql
3个回答
1
投票

isset()数组元素上使用$_POST,以确保它们被设置,例如:

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

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

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

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


0
投票

您检查其他索引而不是您使用的索引。

isset($_POST['redeem_points']) // this will be checked
$id = $_POST['id']; // that is was you are using

0
投票

试试这个:

$id = (int) $_POST['id'];

我想知道,“未定义的索引错误”来自哪里? PHP或JavaScript?因为你的javascript代码也有错误。它应该是这样的;

echo '<script> window.alert("Points Redeemed successfully !");
                        location.href = "admin_manage_points.php" </script>';
© www.soinside.com 2019 - 2024. All rights reserved.