$ _GET不会从URL中检索id

时间:2013-03-17 05:20:27

标签: php

我有一个包含类别的垂直菜单栏,这些类别下面有产品。我将鼠标悬停在该类别上时看到的网址是..../index.php?catid=1

例如我有

category- SCHOOLS (catid=1)
products - NPS, DPS...

为了显示产品,当我将鼠标悬停在产品上时,我需要使用 $ _ GET 从网址获取'catid'。

这是该部分的代码:

$fetchedcatid = $_GET['catid'];
$resultLIst = array();
$i = 0;
$sql="Select * from product_master where id = ".$fetchedcatid;
$query=mysql_query($sql) or die(mysql_error());

这给了我一个MYSQL错误,因为catid没有存储在$ fetchedcatid中。

请帮忙。感谢。

3 个答案:

答案 0 :(得分:2)

你需要先检查catid之前是否设置使用单身也不是好选择

if(isset($_GET['catid'])){
 // other stuff use $_GET['catid']

}

您还可以使用is_intctype_digit来确保ID只有数字


警告

您的代码容易受sql injection攻击,您需要撤消所有getpostrequest,更好的方法是使用Prepared statement

好读

  1. How to prevent SQL injection in PHP?
  2. Are PDO prepared statements sufficient to prevent SQL injection?

  3. 注意

    1. 整个 ext/mysql PHP扩展程序(提供名为mysql_的所有函数)为officially deprecated as of PHP v5.5.0,将来会被删除。因此,请使用PDOMySQLi
    2. 好读

      1. The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead
      2. PDO Tutorial for MySQL Developers
      3. Pdo Tutorial For Beginners

答案 1 :(得分:0)

$_GET['catid']变量

启动测试
$fetchedcatid = isset($_GET['catid']) ? $_GET['catid'] : FALSE;

if($fetchedcatid) { 
  // do stuff here 
  // you will want to sanitize this variable especially when using it in a mysql_query statement
 //For Example:

$resultLIst = array();
$i          = 0;
$sql        = "Select * from product_master where id = ".mysql_real_escape_string($fetchedcatid);
$query      = mysql_query($sql) or die(mysql_error());
}

答案 2 :(得分:0)

我的理解是你遇到了$ _GET变量的问题,而不是它们是否被设置(虽然所有的优点,我认为这不是OP的主要问题)。

尝试:

<?php 

if(empty($_GET)) 
echo "_GET is empty"; 
else 
print_r($_GET); 

?> 

如果你没有得到任何变量,那么你的配置设置有问题,或者_GET被禁用或者是否被框架重新分配给其他变量?

PHP Documentation for GET, POST, and REQUEST