发送String [] b httprequest并获取php b $ _GET [英] send String[] b httprequest and get in php b $_GET
问题描述
我想通过HTTP请求发送String[]
,并使用$_GET
方法获取PHP中的值.
String[]
中的值总数是可变的.
到目前为止,我已经尝试过:
I want to send a String[]
by an HTTP request and get the values in PHP with the $_GET
method.
The total number of values in the String[]
is variable.
I have tried so far:
List<NameValuePair> params = new ArrayList<NameValuePair>();
String[] dropdowns = {"1st item","2nd item","3rd item","4th item"};
for (int i = 0; i < dropdowns.length; i++) {
params.add(new BasicNameValuePair("pid", dropdowns[i]));
}
在PHP中,我想获取所有值并基于它们进行查询.
In PHP I want to get all values and query based on them.
$pid = $_GET['pid'];
并像这样使用它们:
$result = mysql_query("SELECT *FROM Apps WHERE pid[0] = $pid" AND pid[1] = $pid"
AND ...);
但是我知道这种方式是错误的. 我该怎么办?
But I know this way is wrong. How can I do that?
推荐答案
这有两个部分,都涉及循环.首先,在发送数据时,将方括号放在名称中以数组形式发送:
There are two parts to this and both involve loops. First, when you are sending the data, put the brackets in the name to send it as an array:
for (int i = 0; i < dropdowns.length; i++) {
params.add(new BasicNameValuePair("pid[]", dropdowns[i]));
}
第二,在php端,根据您的发送方式,此数组存储在$_GET['pid']
或$_POST['pid']
中,因此您将遍历该数组并将这些项添加到sql查询中.只需创建一个单独的变量来存储sql语句,即可将其添加到其中:
Second, on the php end this array is stored in $_GET['pid']
or $_POST['pid']
depending on how you sent it, so you would loop through the array and add the items to your sql query. Just make a separate variable to store the sql statement so you can add to it:
$x = 0;
foreach($_GET['pid'] as $value) {
$yourSQLString .= " AND pid[". $x ."] = '" . $value . "'";
$x++;
}
显然,您应该对实际值进行其他操作,以避免sql注入.
And obviously you should do something else with the actual value to avoid sql injections.
这篇关于发送String [] b httprequest并获取php b $ _GET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!