无法检索$ _GET ['id']值?

时间:2013-07-25 05:35:35

标签: php html mysql forms get

我无法从$ _GET变量中检索id。我正在尝试使用特定ID更新数据库中的表。问题是,我可以在地址栏上看到id,但无法通过$ _GET检索它,这是代码。在这里,我只分享代码的一部分。请检查并指导我。

<?php
session_start();
include 'connect.php';  
$id= $_GET['product_id'];

$select_query = "select * from products LEFT JOIN product_description 
                ON products.product_id='".$_GET['product_id']."' and 
                product_description.product_id='".$_GET['product_id']."' 
                where products.product_id='".$_GET['product_id']."'     ";

if(!$select_query_run = mysql_query($select_query))
{
    echo mysql_error();
}
else


Notice: Undefined index: product_id in
F:\xampp\htdocs\CMS\update_single_product.php on line 36

如果我改为使用$id,它会在第4行给出错误。

/cms/update_single_product.php?product_id=159

**

  

我做了一些改动,现在正在进行中。我还是不知道怎么回事   得到修复,但其工作:)

**

2 个答案:

答案 0 :(得分:1)

使用isset()来避免问题。

if(isset($_GET['product_id'])) {
  $id= $_GET['product_id'];

} else {
  $id = 1;
}
// code here

使用此功能可以避免在没有获取值的情况下执行操作。

答案 1 :(得分:1)

您应该先检查是否设置了$ _GET ['product_id']。您还在变量中定义了$ _GET ['product_id']。在查询中使用它,并且不要忘记保护所有输入免受mysql注入。

$id = mysql_real_escape_string($_GET['product_id']);

$select_query=  "SELECT * FROM products LEFT JOIN product_description ON products.product_id='".$id."' and product_description.product_id='".$id."' where products.product_id='".$id."'";

您可以尝试回显$ id,看看会发生什么。

旁注; MySQL已被弃用。你应该使用mysqli或PDO。

相关问题