2015-04-18 236 views
2

我的问题是插入到数据库中的值是像éèçàيبلا发送来自Android的特殊字符(字符串)到PHP

这里问号???????发送时字符串我的Java代码

private void uploadFile(String filePath) { 
    try { 
     try { 

      HttpPost httpost = new HttpPost(url_upload); 
      MultipartEntity entity = new MultipartEntity(); 
      entity.addPart("Mdp", new StringBody(mdp)); 
      entity.addPart("Msg", new StringBody("HERE IS THE STRING éçèà")); 
      entity.addPart("type", new StringBody(type)); 
      if(type.equals("1")) { 
       entity.addPart("Emailm", new StringBody(email)); 
       entity.addPart("Emailp", new StringBody(emaildest)); 


    }else { 
       entity.addPart("Emailp", new StringBody(email)); 
       entity.addPart("Emailm", new StringBody(emaildest)); 
      } 
      if (attach){ 
       entity.addPart("attachement", new FileBody(new File(filePath))); 
       entity.addPart("attacher", new StringBody("oui")); 
      }else{ 
       entity.addPart("attacher", new StringBody("non")); 
      } 

      httpost.setEntity(entity); 
      HttpResponse response; 
      HttpClient httpclient = new DefaultHttpClient(); 
      response = httpclient.execute(httpost); 

      if (response != null) { 
       HttpEntity entity2 = response.getEntity(); 
       String responseString = EntityUtils.toString(entity2, "UTF-8"); 
       System.out.println(responseString); 
       json2=new JSONObject(responseString); 

      } else { //erreur 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    }catch (Exception e){ 

    } 
    } 

PHP代码

<?php 
    date_default_timezone_set ("Africa/Algiers"); 
    include('connect.php'); 
    $response = array(); 

    if (isset($_REQUEST['Emailm'])&& isset($_REQUEST['Mdp'])&&isset($_REQUEST['type']) &&  isset($_REQUEST['Emailp'])&&isset($_REQUEST['Msg']) ) { 


    $Emailm = $db->real_escape_string($_REQUEST['Emailm']); 
    $Emailp = $db->real_escape_string($_REQUEST['Emailp']); 
    $Mdp = $db->real_escape_string($_REQUEST['Mdp']); 
     $Msg=$db->real_escape_string($_REQUEST['Msg']); 
     $Mdp = $_REQUEST['Mdp']; 
     $type= $_REQUEST['type']; 
    $target_path1 = "attachement/"; 


    if($type=="0"){ //compte patient 
    if ($result = $db->query("SELECT * FROM `patient` WHERE `Email_p`='$Emailp' AND `Mdp`='$Mdp'")) { 
    $row_cnt = $result->num_rows; } 

    }else { //compte médecin 
    if ($result = $db->query("SELECT * FROM `med` WHERE `Email`='$Emailm' AND `Mdp`='$Mdp'")) { 
    $row_cnt = $result->num_rows; } 
    } 


    $b=false; 
    $a=false; 

    if ($row_cnt>0) { 
       if($_REQUEST['attacher']=="oui"){ 
       $filecount = 0; 
       $files = glob($target_path1 . "*"); 
       if ($files){ 
       $filecount = count($files); 
       } 
       $nomFichier=$filecount.basename($_FILES['attachement']['name']) . ".a"; 
       $target_path1 = $target_path1 . $nomFichier; 
       $nomFichierOrigin=basename($_FILES['attachement']['name']); 
       if(move_uploaded_file($_FILES['attachement']['tmp_name'], $target_path1)) { 
       $b=true;} 
       } 
       else{ 
       $nomFichier=""; 
       $nomFichierOrigin=""; 
       }; 

         $h=new DateTime("now") ; 
         $s=$h->format('G:i:s'); 
         $s2=$h->format('Y-m-d'); 
        if($result = mysqli_query($db,"INSERT INTO `msg`(`cle`, `email_m`, `email_p`, `message`, `attachement`, `type`, `attachementNomOrigin`, `heure`, `date`,`vu`) 
        VALUES ('','$Emailm','$Emailp','$Msg','$nomFichier','$type','$nomFichierOrigin','$s','$s2','non')")) 
        {$a=true;} 

        if($a==true && $b==true && $_REQUEST['attacher']=="oui"){ 
        $dernierId = $db->insert_id; 
        if ($result = $db->query("SELECT * FROM `med` WHERE `Email`='$Emailm' AND `Mdp`='$Mdp'")) { 
        if (mysqli_num_rows($result) > 0) { 
         $response["Msg"] = array(); 
        while ($row = mysqli_fetch_array($result)) { 
        $Msg = array(); 
        $Msg["cle"] = intval($row["cle"]); 
        $Msg["heure"] = $row["heure"]; 
        $Msg["date"] = $row["date"]; 
        $Msg["email_m"] = $row["email_m"]; 
        $Msg["email_p"] = $row["email_p"]; 
        $Msg["message"] = $row["message"]; 
        $Msg["type"] = $row["type"]; 
        $Msg["attachement"] = $row["attachement"]; 
        $Msg["attachementNomOrigin"] = $row["attachementNomOrigin"]; 
        array_push($response["Msg"], $Msg); 
        } 

        $response["success"] = 1; 
        $response["message"] = "Succès"; 
        }}} else 

        if($a==true && $_REQUEST['attacher']=="non"){ 
        $response["success"] = 1; 
        $response["message"] = "Succès"; 
        }else{ 

        $response["success"] = 0; 
        $response["message"] = "Erreur lors de l'envoi"; 
        } 

        echo json_encode($response); 
     } else { 
     $response["success"] = 0; 
     $response["message"] = "Email ou Mot de passe incorrect"; 

     echo json_encode($response); 
     } 
     }else { 
    $response["success"] = 0; 
    $response["message"] = "Champs manqués"; 

     echo json_encode($response); 
     } 
     ?> 

这里的connect.php

<?php 
$db = new mysqli('localhost', 'root', '', 'rechmed'); 
mysqli_set_charset($db,'utf8'); 
?> 

太感谢你了,我花了几天寻找一种方式来解决这个..

回答

0

漫长的探索后,我发现了许多成果......他们都不固定我的问题..但这些结果我得到了什么可能会导致probleme想法..所以大家谁具有相同probleme做到这一点:

A/首先检查你的数据库(我的问题在这里)。 在phpMyAdmin创建数据库时选择utf8_general_ci 见截图:http://i62.tinypic.com/2ards7.png ,这里是我的表结构:https://www.dropbox.com/s/d0hgdrbx14cbl85/rechmed.sql?dl=0

B/转到Java源代码,并添加这样的:(对我来说)

而不是
entity.addPart("Msg", new StringBody(URLEncoder.encode("HERE IS THE STRING éçèà")); 

entity.addPart("Msg", new StringBody("HERE IS THE STRING éçèà")); 

,而不是发布 “你的字符串” 后“URLEn coder.encode(MSG))”

C/现在去你的PHP代码,并在数据库中插入之前在我的情况你解码URL编码字符串形式 (): 现在我用这个:

$Msg = $_REQUEST['Msg']; 
$Msg= $db->real_escape_string(htmlentities(urldecode($Msg))); 

,而不是只使用:

$Msg = $_REQUEST['Msg']; 

这是我的Java代码:

private void uploadFile(String filePath) { 
     try { 
      try { 

       HttpPost httpost = new HttpPost(url_upload); 
       MultipartEntity entity = new MultipartEntity(); 
       entity.addPart("Mdp", new StringBody(mdp)); 
       entity.addPart("Msg", new StringBody(URLEncoder.encode(msg))); 
       entity.addPart("type", new StringBody(type)); 
       if(type.equals("1")) { 
        entity.addPart("Emailm", new StringBody(email)); 
        entity.addPart("Emailp", new StringBody(emaildest)); 
       }else { 
        entity.addPart("Emailp", new StringBody(email)); 
        entity.addPart("Emailm", new StringBody(emaildest)); 
       } 
       if (attach){ 
        entity.addPart("attachement", new FileBody(new File(filePath))); 
        entity.addPart("attacher", new StringBody("oui")); 
       }else{ 
        entity.addPart("attacher", new StringBody("non")); 
       } 

       httpost.setEntity(entity); 
       HttpResponse response; 
       HttpClient httpclient = new DefaultHttpClient(); 
       response = httpclient.execute(httpost); 

       if (response != null) { 
        HttpEntity entity2 = response.getEntity(); 
        String responseString = EntityUtils.toString(entity2, "UTF-8"); 
        System.out.println(responseString); 
        json2=new JSONObject(responseString); 

       } else { //erreur 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     }catch (Exception e){ 

     } 
    } 

,这是PHP代码:

<?php 
date_default_timezone_set ("Africa/Algiers"); 
include('connect.php'); 
$response = array(); 

if (isset($_REQUEST['Emailm'])&& isset($_REQUEST['Mdp'])&&isset($_REQUEST['type']) && isset($_REQUEST['Emailp'])&&isset($_REQUEST['Msg']) ) { 


    $Emailm = $db->real_escape_string($_REQUEST['Emailm']); 
    $Emailp = $db->real_escape_string($_REQUEST['Emailp']); 
    $Mdp = $db->real_escape_string($_REQUEST['Mdp']); 
    $Msg = $_REQUEST['Msg']; 
    $Mdp = $_REQUEST['Mdp']; 
    $Msg= $db->real_escape_string(htmlentities(urldecode($Msg)));  
    $type= $_REQUEST['type']; 
    $target_path1 = "attachement/"; 
    if($type=="0"){ //compte patient 
    if ($result = $db->query("SELECT * FROM `patient` WHERE `Email_p`='$Emailp' AND `Mdp`='$Mdp'")) { 
    $row_cnt = $result->num_rows; } 
    }else { //compte médecin 
    if ($result = $db->query("SELECT * FROM `med` WHERE `Email`='$Emailm' AND `Mdp`='$Mdp'")) { 
    $row_cnt = $result->num_rows; } 
    } 

    $b=false; 
    $a=false; 

    if ($row_cnt>0) { 
       if($_REQUEST['attacher']=="oui"){ 
       $filecount = 0; 
       $files = glob($target_path1 . "*"); 
       if ($files){ 
       $filecount = count($files); 
       } 
       $nomFichier=$filecount.basename($_FILES['attachement']['name']) . ".a"; 
       $target_path1 = $target_path1 . $nomFichier; 
       $nomFichierOrigin=basename($_FILES['attachement']['name']); 
       if(move_uploaded_file($_FILES['attachement']['tmp_name'], $target_path1)) { 
       $b=true;} 
       } 
       else{ 
       $nomFichier=""; 
       $nomFichierOrigin=""; 
       }; 

         $h=new DateTime("now") ; 
         $s=$h->format('G:i:s'); 
         $s2=$h->format('Y-m-d'); 
        if($result = mysqli_query($db,"INSERT INTO `msg`(`cle`, `email_m`, `email_p`, `message`, `attachement`, `type`, `attachementNomOrigin`, `heure`, `date`,`vu`) 
        VALUES ('','$Emailm','$Emailp','$Msg','$nomFichier','$type','$nomFichierOrigin','$s','$s2','non')")) 
        {$a=true;} 

        if($a==true && $b==true && $_REQUEST['attacher']=="oui"){ 
        $dernierId = $db->insert_id; 
        if ($result = $db->query("SELECT * FROM `med` WHERE `Email`='$Emailm' AND `Mdp`='$Mdp'")) { 
        if (mysqli_num_rows($result) > 0) { 
         $response["Msg"] = array(); 
        while ($row = mysqli_fetch_array($result)) { 
        $Msg = array(); 
        $Msg["cle"] = intval($row["cle"]); 
        $Msg["heure"] = $row["heure"]; 
        $Msg["date"] = $row["date"]; 
        $Msg["email_m"] = $row["email_m"]; 
        $Msg["email_p"] = $row["email_p"]; 
        $Msg["message"] = $row["message"]; 
        $Msg["type"] = $row["type"]; 
        $Msg["attachement"] = $row["attachement"]; 
        $Msg["attachementNomOrigin"] = $row["attachementNomOrigin"]; 
        array_push($response["Msg"], $Msg); 
        } 

        $response["success"] = 1; 
        $response["message"] = "Succès"; 
        }}} else 

        if($a==true && $_REQUEST['attacher']=="non"){ 
        $response["success"] = 1; 
        $response["message"] = "Succès"; 
        }else{ 

        $response["success"] = 0; 
        $response["message"] = "Erreur lors de l'envoi"; 
        } 

        echo json_encode($response); 
    } else { 
     $response["success"] = 0; 
     $response["message"] = "Email ou Mot de passe incorrect"; 

     echo json_encode($response); 
    } 
}else { 
    $response["success"] = 0; 
    $response["message"] = "Champs manqués"; 

    echo json_encode($response); 
} 
?> 

这里的connect.php,允许数据库的连接:

<?php 
$db = new mysqli('localhost', 'root', '', 'rechmed'); 
mysqli_set_charset($db,'utf8'); 
mysqli_query($db,"SET NAMES 'utf8'"); 
mysqli_query($db,"SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'"); 

?> 
0

在PHP代码中,尝试使用utf8_decode($ _ REQUEST [“request_value”])每次尝试检索时间即将到来的价值。

+0

它并不能帮助..我觉得问题是在javacode –

+0

好吧,mybe ....在一个开发中,我有同样的问题...“utf8_decode”帮助我解决这个问题。如果你尝试“utf8_decode”,并不能帮助你解决你的问题..所以....对不起。目前没有更多的解决方案。 –

+0

看到我的答案@ToukeaTatsi –