从编辑文本中获取符号并将其保存在数据库中


taking a symbol from edittext and save it in database

我正在尝试从我的 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();
}
}
?>

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

好吧,我想我有你的问题。

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

mysql_set_charset('utf8',$link);

这是因为mysql_query("SET NAMES 'utf8'");告诉数据库需要什么字符集,但 mysql 不会使用该字符集发送它。让我知道这是否有效。

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

此外,您应该知道 mysql_* 函数已弃用。您应该使用 mysqli_PDO

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

这里有一个馅饼,你可以用它来查看我所做的更改。