php - 连接PHP与Android检查Mysql数据库中是否存在值

标签 php android mysql

首先是我的 php 脚本:

<?php

/*
 * Following code will update a product information
 * A product is identified by product id (pid)
 */

// array for JSON response
$response = array();


    // include db connect class
    require_once __DIR__ . '/db_connect.php';

    // connecting to db
    $db = new DB_CONNECT();

// check for required fields
if (isset($_POST['uid']) && isset($_POST['name'])) {

    $uid = $_POST['uid'];
    $name = $_POST['name'];

    $look = mysql_query("SELECT name FROM users WHERE name ='$name'");
    if(mysql_num_rows($look) == 0){

    // mysql update row with matched pid
    $result = mysql_query("UPDATE users SET name = '$name' WHERE uid = $uid");



    // check if row inserted or not
    if ($result) {
        // successfully updated
        $response["success"] = 1;
        $response["message"] = "Product successfully updated.";

        // echoing JSON response
        echo json_encode($response);
    } else {

    }
} else {

    $response["success"] = 2;
    $response["message"] = "Email already exists! :-(";
}
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}

?>

所以我检查该名称是否已存在。如果是,那么“成功”等于 2,如果不是,那么它等于 0,并且应该更新该行。但我在 Android 中遇到错误。如果该名称尚不存在,则它可以正常工作,但如果该值存在,则应用程序会崩溃。以下是 Android 中的 Async-Task 类:

/**
 * Background Async Task to  Save product Details
 */
class SaveProductDetails extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        Toast.makeText(getApplicationContext(), "saving..", Toast.LENGTH_SHORT).show();
        // Displays the progress bar for the first time.
        mBuilder.setProgress(100, 0, true);
        mNotifyManager.notify(id, mBuilder.build());
    }

    /**
     * Saving product
     */
    protected String doInBackground(String... args) {

        // getting updated data from EditTexts
        String name = editTextUserName.getText().toString();

        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair(StaticVariables.UID, idOfCustomer));
        params.add(new BasicNameValuePair(StaticVariables.NAME, name));

        // sending modified data through http request
        // Notice that update product url accepts POST method
        JSONObject json = jsonParser.makeHttpRequest(StaticVariables.url_update_username,
                "POST", params);

        // check json success tag
        try {
            int success = json.getInt(StaticVariables.TAG_SUCCESS);

            if (success == 1) {
                // successfully updated
                Intent i = getIntent();
                // send result code 100 to notify about product update
                setResult(100, i);
                finish();
            } else if (success == 2) {
                // failed to update product
                Toast.makeText(getApplicationContext(), json.getString("message"), Toast.LENGTH_SHORT).show();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * After completing background task Dismiss the progress dialog
     **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog once product uupdated
        // dismiss the dialog once product uupdated
        Toast.makeText(getApplicationContext(), "succesfully", Toast.LENGTH_SHORT).show();
        mBuilder.setContentText("Upload complete");
        // Removes the progress bar
        mBuilder.setProgress(0, 0, false);
        mNotifyManager.notify(id, mBuilder.build());
        finish();
    }
}

最佳答案

你缺少:

echo json_encode($response);

在这种情况下:

else {

    $response["success"] = 2;
    $response["message"] = "Email already exists! :-(";
}

关于php - 连接PHP与Android检查Mysql数据库中是否存在值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32937310/

相关文章:

java - Fragment 的 setActivityForResult 返回空数据

java - 适用于 Android 的 SAML 客户端?

java - Android 向上按钮不起作用

mysql - js $.ajax 调用 perl,需要显示返回 fetchall_arrayref 的 perl

mysql - 解释计划在mysql中的含义

php - mysql查询中出现 "isn' t in GROUP BY”错误如何解决

php - 该数据是否被另一个组件覆盖?

php - 使用 json 中的子句从数据库读取数据

php - 如何在 PostgreSQL 9.1 中使用 pgFouine?

mysql - 在 Windows 10 上的 MySQL Server 5.7 中,更新无法远程运行