【问题标题】:PHP GET or POST data into variable includingPHP GET 或 POST 数据到变量中,包括
【发布时间】:2020-05-27 05:51:02
【问题描述】:

想要将动态 GET 变量转换为两个内部变量,以便我可以在 SQL 查询中使用这些值。

我正在将一个变量字符串传递给这样的 URL

http://localhost/api-data?serial=1923473
http://localhost/api-data?manufacturer=jaguar
http://localhost/api-data?location=london
http://localhost/api-data?country=uk

我想将 GET 数据转换成两个不同的变量,例如 serial 变成 $data1,1923473 变成 $data2。 GET 数据始终采用上述格式我只想转换成两个不同的变量。

print_r($_GET);

我可以看到变量像数组一样传递。我的问题,如果数据以下列格式 AAAAA=BBBBB 作为获取变量传递,我如何将 AAAAA 转换为变量 $data1 并将 BBBBBB 转换为 £data2。请记住,GET 数据始终是唯一的。

一旦我将这些数据包含在 2 个唯一变量中,我想运行 SQL 查询。

select blah1, blah2, blah4, blah5 from datastore where $data1 = "$data2";

提前谢谢你。

【问题讨论】:

  • if(isset($_GET["serial"])) { $data1 = "serial"; $data2 = $_GET["serial"]; } 可能...但是您确实需要使用准备好的语句stackoverflow.com/questions/60174/…
  • 我知道它会起作用,但问题是我不知道用户会传递什么数据,串行就是一个例子,用户可以发布或发送 GET 任何唯一数据。
  • 您的数据库中只有某些列,因此您需要检查是否允许获取 var。
  • 问题就在这里where $data1 = "$data2";你不能“参数化”列名。
  • @DinoCoderSaurus:但是你可以得到数据。

标签: php variables dynamic


【解决方案1】:

你的方法不是最好的,当你有多个 GET 变量时需要重新设计,但总的来说:

$allowed = ['serial', 'manufacturer']; //etc...
$col = key($_GET);

if(in_array($col, $allowed)) {
    $val = $_GET[$col];

    //Then prepare and execute using whatever DB library you are using
    $st = $db->prepare("SELECT blah1, blah2, blah4, blah5 FROM datastore WHERE $col = ?");
    $st->execute([$val]);
}

【讨论】:

    【解决方案2】:

    $_GET 是一个包含所有传递参数的数组。因此,?abc=def&foo=bar 的 URL 将导致 $_GET

    array(2) { 
        ["abc"]=> string(3) "def" 
        ["foo"]=> string(3) "bar" 
    }
    

    使用它,您可以遍历每个项目并将其附加到查询中:

    foreach($_GET as $key => $val) {
        $query .= " AND $key = '$val'";
    }
    

    但是,请务必考虑 SQL 注入。在这种情况下应对这种情况的最佳选择是使用有效密钥列表验证每个密钥。

    【讨论】:

      【解决方案3】:

      这是一种以简单声明性方式的方式。

      我强烈建议检查 PDO 和 PDOStatement 来构建查询。没有添加此示例,因为它不是问题。

      <?php
      
      // given 
      $_GET = ['serial' => 342, 'something-else' => 'not used'];
      $allowedVariables = ['serial', 'manufacturer', 'location', 'country'];
      
      // filter values from the query for only allowed keys
      $data = array_filter($_GET, function ($value, $key) use ($allowedVariables) {
          return in_array($key, $allowedVariables);
      }, ARRAY_FILTER_USE_BOTH);
      
      // create an array of strings like "$data1 = $data2"
      $query = array_map(function ($key, $value) {
          return "$key = $value";
      }, array_keys($data), array_values($data));
      
      // build the query while combining values with the "AND" keyword
      $query = "select * from datastore where " . implode(' and ', $query);
      
      var_dump($query);
      // string(42) "select * from datastore where serial = 342"
      
      

      【讨论】:

      • 虽然这行得通,但它确实有太多的循环和遍历数组的指令。如果这很重要,我在这里担心性能。统计数据显示:1xarray_filter、1xin_array、1xarray_map、1xarray_keys、1xarray_values、1ximplode
      • @asiby 你是完全正确的。我们也可以考虑验证和消毒,但所有这些都不是问题。试图在不带个人偏见的情况下尽可能地回答。
      • 我了解@christoph-kluge。我认为我的评论是错误的。看,我在平台上工作,在任何给定时刻,你都有超过 10,000 个连接到运行此类脚本的系统某些部分。在这种情况下,您会惊讶地看到一些单一指令对性能的影响。正因为如此,我倾向于建议人们在精简、可读、高效、安全和可维护的代码之间找到平衡。这就是我发表评论的动机。
      【解决方案4】:

      为此,您可以使用extract($inputArray)(在php.net 上阅读更多信息)

      输入数组中的键将成为变量名,它们的值将分配给这些新变量。

      另外,如果您想过滤键,以免有人在您当前范围内注入不需要的变量,请在调用 extract() 函数之前执行以下操作...

      <?php
      // Pick only the data keys that are expected. Their names must be compatible with the PHP variable naming conventions.
      $filteredData = array_intersect_key($_GET /* or $_POST*/, array_flip(['serial', 'manufacturer', 'location', 'country']));
      
      // Extract the names into variables in the current scope.
      extract($filteredData);
      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-22
        • 2016-07-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多