如果变量是通过 URL 传递的,我将使用以下代码为变量赋值:
if (!empty($_POST)) {
$sortBy = isset($_GET['sort']) ? $_GET['sort'] : 'mgap_ska_id';
}
这里是传递变量的地方:
$result = "SELECT mgap_ska_id, mgap_ska_id_name, SUM(DISTINCT mgap_growth) as growthtotal, SUM(DISTINCT mgap_recovery) as recovery FROM mgap_orders "
. "WHERE account_manager_id = '" . $_SESSION['account_manager_id'] . "' GROUP BY mgap_ska_id ORDER BY '".$sortBy. "' ";
如果变量没有被传递,这个过程工作正常,但是当通过这样的链接传递时,查询中的变量变得未分配:
<a href = "customer_view.php?sort=mgap_ska_id_name">
谁能看出我的错误在哪里?
仅供引用,我在查询中尝试过使用和不使用引号。
用解决方案编辑:
这是我解决问题的方法:O 检查了 POST 和 GET:
if (!empty($_GET) || !empty($_POST)) {
$sortBy = isset($_GET['sort']) ? $_GET['sort'] : 'mgap_ska_id';
} else {
$sortBy = 'mgap_ska_id';
}
最佳答案
来自 OP 的 comments
The page needs to check for a POST because it can receive such from another entry point, but the URL sending the variable is on the same page.I have to check the URL for the existence of the variable. –
然后利用$_REQUEST
if (!isset($_REQUEST)) {
关于php - $_GET 变量是查询在传递时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23810564/