$ _POST没有在mysql数据库上工作

时间:2015-11-21 19:56:55

标签: php android mysql json database

当我尝试将两个变量传递给mysql数据库以从表中选择所有名称和姓氏时,我遇到了问题,但仅限于给定的用户名和密码(我传递的变量)。
如果我没有使用任何$ _POST,程序会正确地获取我在Contacts表中获得的姓名和姓氏。但那不是我需要的。我实际上只需要为给定的用户名和密码取回姓名和姓氏。

这是我的代码

    public class ApiConnector {  
public JSONArray GetAllCustomers(User user){
String url = "http://giacomoci.co.uk/FetchallDataList.php";
ArrayList<NameValuePair> dataToSend = new ArrayList<>();
dataToSend.add(new BasicNameValuePair("username", user.username));
dataToSend.add(new BasicNameValuePair("password", user.password));
HttpParams httpRequestParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpRequestParams, CONNECTION_TIMEOUT);

DefaultHttpClient httpClient = new DefaultHttpClient(httpRequestParams);
HttpPost post = new HttpPost(url);

JSONArray jsonArray = null;

try {


    //DefaultHttpClient httpClient = new DefaultHttpClient();
    //HttpGet httpGet = new HttpGet(url);
    post.setEntity(new UrlEncodedFormEntity(dataToSend));


    HttpResponse httpResponse = httpClient.execute(post);

    HttpEntity httpEntity = httpResponse.getEntity();

    if(httpEntity != null){

        try {

            String entityResponse = EntityUtils.toString(httpEntity);
            Log.e("Entity Response : ", entityResponse);

            jsonArray = new JSONArray(entityResponse);
        }catch (JSONException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
} catch (ClientProtocolException e){
    e.printStackTrace();
} catch (IOException e){
    e.printStackTrace();
}

return jsonArray;
    }}

和PHP文件

    $username = $_POST["username"];
    $password = $_POST["password"];
    $statement = mysqli_prepare($con, "SELECT name, surname FROM Contacts
      WHERE username = ? AND password = ?");
    mysqli_stmt_bind_param($statement, "ss", $username, $password);
    $user[] = array();
    while($row = mysqli_fetch_assoc($statement)){
    $user[] = $row;}
    echo json_encode($user);
    mysqli_close($con);

0 个答案:

没有答案
相关问题