从edittext中获取符号并将其保存在数据库中

时间:2014-12-17 15:12:52

标签: php android

我正在尝试从我的Android应用程序中的EditText中取一个符号并将其保存在数据库中(它是从数据库中检索出来的,我正在编辑它)。例如,我有这个符号:μ。它会被很好地检索,但是当我编辑它并保存更改时,符号将保存为?,我不知道为什么。

我已将此添加到我的PHP文件中:

mysql_query("SET NAMES 'utf8'");
header("Content-type: application/json; charset=utf-8");

这是Java代码

class SaveglossaryDetails extends AsyncTask<String, String, String> {


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

    // getting updated data from EditTexts
    String Name = name.getText().toString();
    String Description = description.getText().toString();
    String Symbol = symbol.getText().toString();

    // Building Parameters
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair(TAG_GID, gid));
    params.add(new BasicNameValuePair(TAG_NAME, Name));
    params.add(new BasicNameValuePair(TAG_DESCRIPTION, Description));
    params.add(new BasicNameValuePair(TAG_SYMBOL, Symbol));

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

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

        if (success == 1) {
            // successfully updated
            Intent i = new Intent(getApplicationContext(), AdminGlossary.class);
            i.putExtra("admin", admin);
            startActivity(i);
        } else {
            // failed to update glossary
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return null;
}

我的PHP代码

<?php


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

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

$ID_glossary = $_POST['ID_glossary'];
$name = $_POST['name'];
$symbol = $_POST['symbol'];
$description = $_POST['description'];


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

// connecting to db
$db = new DB_CONNECT();
mysql_query("SET NAMES 'utf8'");

// mysql update row with matched pid
$result = mysql_query("UPDATE glossary SET Symbol='$symbol', Name = '$name',     Description = '$description' WHERE ID_Glossary = $ID_glossary");
header("Content-type: application/json; charset=utf-8");

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

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

}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";

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

db_connect

<?php

/**
 * A class file to connect to database
 */
class DB_CONNECT {

// constructor
function __construct() {
    // connecting to database
    $this->connect();
}

// destructor
function __destruct() {
    // closing db connection
    $this->close();
}

/**
 * Function to connect with database
 */
function connect() {
    // import database connection variables
    require_once __DIR__ . '/db_config.php';

    // Connecting to mysql database
    $con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysql_error());

    // Selecing database
    $db = mysql_select_db(DB_DATABASE) or die(mysql_error()) or die(mysql_error());
mysqli_query($link, "SET SESSION CHARACTER_SET_RESULTS =utf8;");
mysqli_query($link, "SET SESSION CHARACTER_SET_CLIENT =utf8;");
    // returing connection cursor
    return $con;
}

/**
 * Function to close db connection
 */
function close() {
    // closing db connection
    mysql_close();
}

}

?>

我该如何解决这个问题?问题出在哪里?

2 个答案:

答案 0 :(得分:0)

好吧,我想我遇到了你的问题。

在服务器端的每个插入查询之前,请尝试添加

mysql_set_charset('utf8',$link);

这是因为mysql_query("SET NAMES 'utf8'");告诉数据库期望什么字符集,但是mysql没有使用该字符集发送它。如果有效,请告诉我。

这可能不是问题,但是如果你在php脚本上执行mysql_client_encoding($link);并且它没有返回utf8,那么这很可能是问题所在。

此外,您应该知道不推荐使用 mysql _ * 函数。您应该使用 mysqli _ PDO

Source

答案 1 :(得分:0)

好了,我已经看到了更多代码,我发现你做了很多错事。

您可以使用pastie来查看我所做的更改。