简体   繁体   English

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

[英]taking a symbol from edittext and save it in database

I'm trying to take a symbol from an EditText in my Android application and save it in the database (it's retrieved from the database and I'm editing it). 我正在尝试从Android应用程序中的EditText符号并将其保存在数据库中(它是从数据库中检索到的,我正在对其进行编辑)。 For example, I have this symbol: μ . 例如,我有这个符号: μ It will be retrived fine, but when I edit it and save the changes the symbol will be saved as a ? 可以很好地检索它,但是当我编辑它并保存更改时,该符号将另存为? , and I don't know why. ,我也不知道为什么。

I've added this to my PHP file: 我已将此添加到我的PHP文件中:

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

and this is the Java code 这是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;
}

my php code 我的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);
}
?>

the db_connect 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();
}

}

?>

How can I solve this, and where is the problem? 我该如何解决?问题出在哪里?

Alright, I think I've got your problem. 好吧,我想我有你的问题。

Before every insert query on the server side, try adding 在服务器端进行每个插入查询之前,请尝试添加

mysql_set_charset('utf8',$link);

This is because mysql_query("SET NAMES 'utf8'"); 这是因为mysql_query("SET NAMES 'utf8'"); tells the database what charset to expect, but the mysql is not sending it with that charset. 告诉数据库期望使用什么字符集,但是mysql没有使用该字符集发送它。 Let me know if this works. 让我知道这个是否奏效。

This may not be the problem, but if you execute mysql_client_encoding($link); 可能不是问题,但是如果执行mysql_client_encoding($link); on the php script and it does not return utf8 then this is most likely the issue. 在PHP脚本上,它不返回utf8那么这很可能是问题。

Also, you should know that mysql_* functions are deprecated. 另外,您应该知道不建议使用mysql_ *函数。 You should use mysqli_ or PDO . 您应该使用mysqli_PDO

Source 资源

Ok now that I've seen more code, I see you're doing alot of things wrong. 好吧,现在我看到了更多的代码,我看到您在做很多错误的事情。

Here's a pastie you can use to see the changes I've made. 这是一个馅饼,您可以用来查看我所做的更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM