我想要的是,当某人(用户)单击主事件上的按钮时,两个值将在 volley POST 请求的帮助下发送到 php 文件,例如 minvalue 和 maxvalue,并且它将从 mysql 中相应地选择数据数据库。
这是我的 POST 请求代码:
public void FilterPower(View view) {
StringRequest stringRequest = new StringRequest(Request.Method.POST, Config.DATA_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
/*Some Method*/
/*Display data in recyclerview*/
//Initializing Views
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
//Initializing our superheroes list
listSuperHeroes = new ArrayList<>();
//Calling method to get data
getData();
/*End Display data in recyclerview*/
/*End Some Method*/
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String,String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("minvalue", "1000");
params.put("maxvalue", "2000");
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
requestQueue.add(stringRequest);
}
}
我的 PHP 文件代码:
<?php
$con = mysqli_connect("127.0.0.1","superinfoadmin","","superhero");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
if($_SERVER['REQUEST_METHOD']=='POST'){
$minvalue = $_POST['minvalue'];
$maxvalue = $_POST['maxvalue'];
$result = mysqli_query($con, "SELECT * FROM superinfo, publisher WHERE superinfo.s_publisherid=publisher.p_id AND (s_power BETWEEN $minvalue AND $maxvalue)");
//$result = mysqli_query($con, "INSERT INTO testDB (min, max) VALUES ('$minvalue', '$maxvalue')");
while($row = mysqli_fetch_assoc($result))
{
$output[]=$row;
}
print(json_encode($output));
mysqli_close($con);
}
?>
当我尝试使用上面的代码运行我的应用程序时,没有选择数据并在 recyclerview 中显示数据。
但是当我尝试使用相同的变量在数据库中插入值时,这些值就会被插入。
我什至尝试保留 select 和 insert 语句来检查它是否进入“IF”循环,是的,它确实进入并将值插入到数据库中,但不执行 select 语句。
但是,如果我给出一个 else 语句来选择所有数据并将其显示在 recyclerview 中,则它可以正常工作,因此在 recyclerview 中显示数据没有问题,代码可以正常工作。下面是带有 else 语句的相同 PHP 代码:
if($_SERVER['REQUEST_METHOD']=='POST'){
$minvalue = $_POST['minvalue'];
$maxvalue = $_POST['maxvalue'];
$result = mysqli_query($con, "SELECT * FROM superinfo, publisher WHERE superinfo.s_publisherid=publisher.p_id AND (s_power BETWEEN $minvalue AND $maxvalue)");
//$result = mysqli_query($con, "INSERT INTO testDB (min, max) VALUES ('$minvalue', '$maxvalue')");
while($row = mysqli_fetch_assoc($result))
{
$output[]=$row;
}
print(json_encode($output));
mysqli_close($con);
}
else{
$result = mysqli_query($con, "SELECT * FROM superinfo, publisher WHERE superinfo.s_publisherid=publisher.p_id");
while($row = mysqli_fetch_assoc($result))
{
$output[]=$row;
}
print(json_encode($output));
mysqli_close($con);
}
请帮助我,我不知道自己做错了什么或没做什么。
最佳答案
我在您的代码中没有发现任何问题,但请检查此内容,如果您收到任何输出,请通知我
<?php
$con = mysqli_connect("127.0.0.1","superinfoadmin","","superhero");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
if($_SERVER['REQUEST_METHOD']=='POST'){
$minvalue = $_POST['minvalue'];
$maxvalue = $_POST['maxvalue'];
$sql="SELECT * FROM superinfo, publisher WHERE
superinfo.s_publisherid=publisher.p_id AND s_power BETWEEN '$minvalue' AND
'$maxvalue'";
echo $sql;
$result = mysqli_query($con,$sql);
$output=array();
$output = mysqli_fetch_assoc($result);
print(json_encode($output));
mysqli_close($con);
}
?>
关于php - 使用android volley post请求从MySQL中选择数据并将其显示在recyclerview中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36952952/