无法修复未定义的索引错误

时间:2013-08-25 13:48:00

标签: php get indexing undefined

我一直在寻找解决这个问题的方法已经有一段时间了,我发现的大部分内容都是那些没有检查看到$ _GET变量的人是否有这个错误。但是,我确实检查它们是否已设置但我仍然会出现此错误。这是我的代码:

$customer_id = $_GET['customer_id'];
$actual_customer_id = $_GET['actual_customer_id'];


if (isset($_GET['customer_id'])&& $_GET['actual_customer_id'])    
{    
$query = "SELECT rewards_points.points,customers.id AS customerID, customers.email
 FROM rewards_points,customers WHERE rewards_points.customer_id= $customers_id AND
  $actual_customer_id = rewards_points.actual_customer_id";
}


$query = "SELECT rewards_points.points,customers.id AS customerID, customers.email
 FROM rewards_points,customers";

错误是:

Notice: Undefined index: customer_id in /Applications/MAMP/htdocs/insertintotestt.php on line 14

Notice: Undefined index: actual_customer_id in /Applications/MAMP/htdocs/insertintotestt.php on line 15
[{"points":"20","customerID":"1","email":"someEmail@gmail.com 
 {"points":"20","customerID":"2","email":"otherEmail@gmail.com"},
 {"points":"10","customerID":"1","email":"aha238@gmail.com"},
 {"points":"10","customerID":"2","email":"spgiegg@gmail.com"},
 {"points":"15","customerID":"1","email":"aha238@gmail.com"},
 {"points":"15","customerID":"2","email":"spgiegg@gmail.com"},
 {"points":"25","customerID":"1","email":"aha238@gmail"},
 {"points":"25","customerID":"2","email":"spgiegg@gmail.com"}]

1 个答案:

答案 0 :(得分:3)

您需要检查首先尝试使用变量的时间。

即:

$customer_id = isset($_GET['customer_id']) ? $_GET['customer_id'] : null;
$actual_customer_id = isset($_GET['actual_customer_id']) ? $_GET['actual_customer_id'] : null;

if ($customer_id && $actual_customer_id) { 
    ...

怀疑迫切需要阅读How can I prevent SQL injection in PHP?,但这是猜测,因为我无法看到你如何执行你的SQL查询