php未定义索引id,但if(isset($\u post['id'])语句工作正常

gywdnpxw  于 2021-06-20  发布在  Mysql
关注(0)|答案(3)|浏览(240)

我是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";
                    }
                }
cxfofazt

cxfofazt1#

试试这个:

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

我在想,“未定义的索引错误”从何而来?php还是javascript?因为你的javascript代码也有错误。它应该是这样的;

echo '<script> window.alert("Points Redeemed successfully !");
                        location.href = "admin_manage_points.php" </script>';
wwodge7n

wwodge7n2#

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

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

如果使用PHP7或更高版本,则可以使用以下选项:

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

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

pxq42qpu

pxq42qpu3#

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

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

相关问题