【问题标题】:Can't fix the undefined index error [duplicate]无法修复未定义的索引错误[重复]
【发布时间】:2013-08-25 13:47:59
【问题描述】:

我一直在寻找解决这个问题的方法有一段时间了,我发现的大部分是那些没有检查 $_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"}]

【问题讨论】:

    标签: php get indexing undefined


    【解决方案1】:

    您需要检查何时第一次尝试使用这些变量。

    即:

    $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 查询的。

    【讨论】:

      猜你喜欢
      • 2018-04-14
      • 1970-01-01
      • 1970-01-01
      • 2021-05-12
      • 2012-11-14
      • 2011-12-04
      • 2018-09-11
      • 1970-01-01
      相关资源
      最近更新 更多