【问题标题】:Android cannot insert into database string with accents and blank spaceAndroid 无法插入带有重音符号和空格的数据库字符串
【发布时间】:2015-08-17 19:26:19
【问题描述】:

我对这个字符串有疑问:“Não Vou”。意思是“我不去”。我正在尝试使用 php/json 文件将其插入数据库。

当我删除空格和 ~ 重音时,它可以正常工作。

我尝试过的:

  • 将 iso-8859-1 更改为 utf-8;
  • 将eclipse编码改为utf-8;
  • 将 print(json_encode($flag)) 更改为 print(json_encode('utf8_encode', $flag));
  • 读过类似的帖子,但其中任何一篇都对我有帮助。

    public class insertNo extends AsyncTask<String, Boolean, Boolean>{
    
    @Override
    protected Boolean doInBackground(String... params) {
    
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("e_id", subString));
        nameValuePairs.add(new BasicNameValuePair("Não Vou", no));
    
        try{
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://mywebsite.com/includes/insert_counter.php");
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity entity = httpResponse.getEntity();
            inputStream = entity.getContent();
            Log.e("pass 1", "connection success");
        }catch(Exception e){
            Log.e("Fail 1", e.toString());
            Log.e("Invalid IP", e.toString());
        }
    
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder stringBuilder = new StringBuilder();
            while ((line = reader.readLine()) != null) {                
                stringBuilder.append(line + "\n");
            }
            inputStream.close();
            result = stringBuilder.toString();
            Log.e("pass 2", "connection success");
        } catch (Exception e) {
            Log.e("Fail 2", e.toString());
        }
    
        try {
            JSONObject json_data = new JSONObject(result);
            code = (json_data.getInt("code"));
            if (code==1) {
                Log.d("Insert success", result);
            } else {
                Log.d("Sorry son", result);
            }
        } catch (Exception e) {
            Log.e("Fail 3", e.toString());
        }
        return null;
    }
    

    }

php 文件

include_once("db_connect.php");

$e_id = $_REQUEST["e_id"];
$yes = $_REQUEST["Vou"];
$no = $_REQUEST["Não Vou"];
$maybe = $_REQUEST["Talvez"];

$flag['code']=0;


if(isset($yes)){
    $r = mysqli_query($mysqli, "UPDATE table1 SET yes = yes + 1 WHERE id='$e_id'");
    $flag['code']=1;
}else if(isset($no)){
    $r = mysqli_query($mysqli, "UPDATE table1 SET no = no + 1 WHERE id='$e_id'");
    $flag['code']=1;
}else if(isset($maybe)){
    $r = mysqli_query($mysqli, "UPDATE table1 SET maybe = maybe + 1 WHERE id='$e_id'");
    $flag['code']=1;
}

print(json_encode($flag));
mysqli_close($mysqli);

【问题讨论】:

  • 什么样的数据库?
  • @lispHK01 MySql utf8_unicode
  • 在数据库中你不会看到重音符号,因为被编码了,当你检索数据时你会再次看到它们。此外,在您的更新查询中,如果 [id] 是一个 int 列,您应该使用 $e_id 而不是 '$e_id'
  • @AlbertoFernández 这不是问题所在。我还有 2 个类使用“普通字符串”很好地插入数据:“Vou”和“Talvez”。问题是两个单词之间的重音和空格
  • 请确认您的 PHP 变量是正确的。 This states 你不能有扩展字符,也不能在变量名中有空格。它应该是“Nao_Vou”之类的。检查命名约定的链接。

标签: php android mysqli


【解决方案1】:

From the documentation,PHP 变量不应包含特殊字符、空格或任何超出

的内容
a letter is a-z, A-Z, and the bytes from 127 through 255 (0x7f-0xff). 

【讨论】:

    猜你喜欢
    • 2017-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-01
    相关资源
    最近更新 更多