无法使用isset获取PHP变量

时间:2013-09-05 18:46:59

标签: php xampp

我是PHP的新手,无法使用以下代码:

<?php
session_start();
include('db_connect.php');


//get the page id
if(isset($_SESSION['logged-in'])&&isset($_SESSION['username'])){
    $login = true;
} else {
    $login = false;
}

//check to see if the post_id and the forum_id is set and is a numeric value
if(isset($_GET['pid'])&&is_numeric($_GET['pid'])&&isset($_GET['id'])&&is_numeric($_GET['id'])){
    $pid = $_GET['pid'];
    $id = $_GET['id'];    
} else {
    die("Error");
}
echo $pid;
?>

当我运行此代码时,我的屏幕只显示“错误”。我做了一些测试,看起来像isset和/或is_numeric属性不起作用。我通过XAMPP(1.8.3-0)使用PHP(5.5.1)和MySQL。我真的不明白为什么会有问题,我假设它与mysql有关。我错过了什么?任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

这不会让你的代码单独运行,但我相信你会发现这个技巧在现在和未来都非常有用。将标题为die("Error");的行更改为echo '<pre>'; print_r($_GET); echo '</pre>';。这将打印整个$ _GET数组,让您更好地了解最新情况。

此外,is_numeric()在这里还不够好。尝试将此功能添加到您的代码中:

function isInteger($input){ return(ctype_digit(strval($input))); }

使用此新函数替换对is_numeric()的每次调用。对于诸如“1.25”之类的输入,is_numeric()将返回true。我怀疑你是否在为你的ID字段使用浮点数;)