我尝试将一些字符串发送到SQL数据库,同时根据发送数据需要一些值,这里我想根据用户ID获取用户地址。
这是我的StringRequest:
StringRequest stringRequest = new StringRequest(Request.Method.POST, Server_Url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(CTX,"successful",Toast.LENGTH_SHORT).show();
}
},new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError volleyError){
Toast.makeText(CTX,"Unsuccessful\nTry Again",Toast.LENGTH_SHORT).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> param = new HashMap<String,String>();
param.put("name",name);
param.put("user_id",user_id);
return param;
}
};
Singletone.getSingletone(CTX).addToRequest(stringRequest);
这是我的php文件
$name = $_POST["name"];
$user_id = $_POST["user_id"];
$db_name = "andr1";
$user_name = "user";
$password = "";
$server = "localhost";
$con = mysqli_connect($server,$user_name,$password,$db_name);
$sql = "INSERT INTO user_car VALUES('".name."', '".$user_id."');";
if(mysqli_query($con,$sql)){
echo "Success...";
}
else{
echo "Error...";die("Error: " . mysqli_error($con));
}