更新Mysqli功能不起作用

时间:2017-07-31 10:30:31

标签: php android mysqli

我几乎尝试了一切来解决这个问题,但没有任何帮助我。这是我的代码。它返回成功:0一直是因为UPDATE不能以某种方式工作。问题在哪里?

PHP文件

<?php
include('config.php');

$username = $_POST['username'];
$email = $_POST['email'];

$con = mysqli_connect($host, $username, $password, $database);
$result = mysqli_query($con, "UPDATE `users` SET Username = '$username', PirmasPaleidimas = 0 WHERE Email = '$email'");

if($result) {
    $json['success'] = 1;
} else {
    $json['success'] = 0;
}

mysqli_close($con);
echo json_encode($json);
?>

Android功能:

private class AsyncDataClass extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {

            HttpParams httpParameters = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParameters, 10000);
            HttpConnectionParams.setSoTimeout(httpParameters, 10000);

            HttpClient httpClient = new DefaultHttpClient(httpParameters);
            HttpPost httpPost = new HttpPost(params[0]);

            String jsonResult = "";
            try {
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                nameValuePairs.add(new BasicNameValuePair("username", params[1]));
                nameValuePairs.add(new BasicNameValuePair("email", params[2]));
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                HttpResponse response = httpClient.execute(httpPost);
                jsonResult = inputStreamToString(response.getEntity().getContent()).toString();

            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return jsonResult;
        }
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }
        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            System.out.println("Resulted Value: " + result);
            if(result.equals("")){
                AsyncDataClass asyncRequestObject = new AsyncDataClass();
                asyncRequestObject.execute(serverUrlUsername, username, email);
                return;
            }
            int jsonResult = returnSucces(result);
            if(jsonResult == 0){
                AsyncDataClass asyncRequestObject = new AsyncDataClass();
                asyncRequestObject.execute(serverUrlUsername, username, email);
                System.out.println(username + "      " + email);
                return;
            }
            if(jsonResult == 1) {
                SharedPreferences sharedPreferences = getSharedPreferences("SharedPref", MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString("Username", username);
                editor.putBoolean("Prisijungta", true);
                editor.apply();

                Intent intent = new Intent(PirmasPaleidimas.this, MainActivity.class);
                startActivity(intent);
                finish();
            }

        }
        private StringBuilder inputStreamToString(InputStream is) {
            String rLine = "";
            StringBuilder answer = new StringBuilder();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            try {
                while ((rLine = br.readLine()) != null) {
                    answer.append(rLine);
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return answer;
        }
    }
    private int returnSucces(String result){
        JSONObject resultObject = null;
        int returnedResult = 0;
        try {
            resultObject = new JSONObject(result);
            returnedResult = resultObject.getInt("success");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return returnedResult;
    }

我用这个

调用AsyncTask
submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                email = getIntent().getStringExtra("Email");
                username = username_text.getText().toString();
                AsyncDataClass asyncRequestObject = new AsyncDataClass();
                asyncRequestObject.execute(serverUrlUsername, username, email);
                System.out.println(username + "      " + email);
            }
        });

请记住System.out.println();正确打印详细信息,Android Monitor根本不会抛出任何错误。一切似乎都正常,但我的UPDATE功能不能做我想做的事。

1 个答案:

答案 0 :(得分:-1)

运行此代码,如果有任何错误让我知道 - 刚刚用单引号,displayerrors和mysqli_error添加了concat sql字符串,以便我们找到问题。

<?php
ini_set('display_errors',1);
include('config.php');

$username = $_POST['username'];
$email = $_POST['email'];

$json = array();
$con = mysqli_connect($host, $username, $password, $database);
$result = mysqli_query($con, "UPDATE users SET Username = '".$username."', PirmasPaleidimas = 0 WHERE Email = '".$email."'") or die(mysqli_error($con));

if($result) {
    $json['success'] = 1;
} else {
    $json['success'] = 0;
}

mysqli_close($con);
echo json_encode($json);
?>