简体   繁体   English

PHP GET 或 POST 数据到变量中,包括

[英]PHP GET or POST data into variable including

Want to convert dynamic GET variables to two internal variables so that I can use these values in SQL query.想要将动态 GET 变量转换为两个内部变量,以便我可以在 SQL 查询中使用这些值。

I am passing a variable string to a URL like this我将一个变量字符串传递给这样的 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

I want to convert the GET data into two different variables, for example serial becomes $data1 and 1923473 becomes $data2.我想将 GET 数据转换为两个不同的变量,例如 serial 变为 $data1,1923473 变为 $data2。 GET data is always in above format I just want to convert into two different variables. GET 数据始终采用上述格式,我只想转换为两个不同的变量。

print_r($_GET);

I can see the variable is passed like an array.我可以看到变量像数组一样传递。 My question, if data passed in following format AAAAA=BBBBB as get variable, how do I convert AAAAA into variable $data1 and BBBBBB into £data2.我的问题是,如果数据以以下格式 AAAAA=BBBBB 作为获取变量传递,我如何将 AAAAA 转换为变量 $data1 和 BBBBBB 转换为 £data2。 Bear in mind GET data will always be unique.请记住,GET 数据将始终是唯一的。

Once I have this data in 2 unique variables, I want to run a SQL query.一旦我在 2 个唯一变量中获得这些数据,我想运行 SQL 查询。

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

Thank you in advance.先感谢您。

Your approach is not the best and will need to be reworked when you have more than one GET variable, but in general:您的方法不是最好的,当您有多个 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]);
}

$_GET is an array of all parameters passed. $_GET是所有传递参数的数组。 So, a URL of ?abc=def&foo=bar would result in a $_GET of因此, ?abc=def&foo=bar的 URL 将导致$_GET

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

Using this, you can loop through each item and append it to a query:使用它,您可以遍历每个项目并将其附加到查询中:

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

However, be sure you account for SQL injections.但是,请确保您考虑到 SQL 注入。 The best option for countering this in this scenario is to verify each key with a list of valid keys.在这种情况下,解决此问题的最佳选择是使用有效密钥列表验证每个密钥。

This is one way of doing it in a plain and declarative manner .这是一种以简单明了的声明方式进行操作的方法

I would highly recommend checking PDO and PDOStatement to build the query.我强烈建议检查 PDO 和 PDOStatement 来构建查询。 Didn't add this example because it was not the question.没有添加这个例子,因为这不是问题。

<?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"

For that, you can use extract($inputArray) (read more on php.net )为此,您可以使用extract($inputArray) (在php.net上阅读更多内容)

The keys in the input array will become the variable names and their values will be assigned to these new variables.输入数组中的键将成为变量名称,它们的值将分配给这些新变量。

Also, if you want to filter the keys so that someone does not just inject unwanted variable in your current scope, then do the following before calling the extract() function...此外,如果您想过滤键,以便某人不只是在您当前的作用域中注入不需要的变量,请在调用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);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM