2015-10-02 74 views
0

它有Android應用程序向數據庫添加數據, 但是當你在阿拉伯數據輸入時, 顯示「?????」 。 我嘗試了所有的方法。如何從android中插入阿拉伯數據到mysql數據庫

文件的config.inc.php:

<?php 

$username = "*****"; 
$password = "****"; 
$host = "*****"; 
$dbname = "*****"; 

$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'); 

try 
{ 
    $db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options); 
mysql_set_charset('utf8'); 
} 
catch(PDOException $ex) 
{ 
    die("Failed to connect to the database: " . $ex->getMessage()); 
} 


$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);  
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); 

if(function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) 
{ 
    function undo_magic_quotes_gpc(&$array) 
    { 
     foreach($array as &$value) 
     { 
      if(is_array($value)) 
      { 
       undo_magic_quotes_gpc($value); 
      } 
      else 
      { 
       $value = stripslashes($value); 
      } 
     } 
    } 

    undo_magic_quotes_gpc($_POST); 
    undo_magic_quotes_gpc($_GET); 
    undo_magic_quotes_gpc($_COOKIE); 
} 
?> 

添加文件:

<?php 

require("config.inc.php"); 

$query = "INSERT INTO news ( news_date, news_title, news_body) VALUES (:news_date, :news_title, :news_body) "; 
$query_params = array(
    ':news_date' => $_POST['news_date'], 
    ':news_title' => $_POST['news_title'], 
    ':news_body' => $_POST['news_body'] 
); 



try 
{ 
    $stmt = $db->prepare($query); 
    $result = $stmt->execute($query_params); 

} 
catch (PDOException $ex) 
{ 
    $response["success"] = 0; 
    $response["message"] = "Database Error. Couldn't add News!"; 
    die(json_encode($response)); 
} 

$response["success"] = 1; 
$response["message"] = "News Successfully Added!"; 
echo json_encode($response); 
?> 

在Android項目: 的Java文件:

public class addnews extends Dialog { 

private Activity mActivity; 

private EditText mNewsTitleET; 
private EditText mNewsET; 

private Button mPublishBtn; 
public String username; 

JSONParser jsonParser = new JSONParser(); 

private String ADD_URL = 
     "http://yazanyazan3.esy.es/newssite/addnews.php"; 

public addnews(Activity mActivity) 
{ 
    super(mActivity); 
    this.mActivity = mActivity; 
} 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.activity_addnews); 

    mNewsTitleET = (EditText) findViewById(R.id.title_news); 
    mNewsET  = (EditText) findViewById(R.id.news_box); 
    mPublishBtn = (Button) findViewById(R.id.publish_btn); 

    Bundle use =getIntent().getExtras(); 
    username = use.getString("name"); 

    mPublishBtn.setOnClickListener(new View.OnClickListener() 
    { 
     @Override 
     public void onClick(View view) 
     { 
      attempAdding(); 
     } 
    }); 
} 

private void attempAdding() 
{ 
    if (!mNewsTitleET.getText().toString().equals("") && 
      !mNewsET.getText().toString().equals("")) 
    { 
     new AddNewsTask().execute(); 
    } 
    else 
    { 
     Toast.makeText(mActivity.getApplicationContext(), 
       "All fields are requires", Toast.LENGTH_LONG).show(); 
    } 
} 


public Intent getIntent() { 
    return mActivity.getIntent(); 
} 


private class AddNewsTask extends AsyncTask<Void, Void, Boolean> 
{ 
    private ProgressDialog mProgressDialog; 

    private JSONObject jsonObjectResult = null; 

    private String error; 

    @Override 
    protected void onPreExecute() 
    { 
     super.onPreExecute(); 
     mProgressDialog = ProgressDialog.show(addnews.this.getContext(), 
       "Processing...", "Adding new news", false, false); 
    } 

    @Override 
    protected Boolean doInBackground(Void... params) 
    { 
     DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 
     Date date = new Date(); 


     List<NameValuePair> pairs = new ArrayList<NameValuePair>(); 
     pairs.add(new BasicNameValuePair("news_user",username)); 
     pairs.add(new BasicNameValuePair("news_date", dateFormat.format(date).toString())); 
     pairs.add(new BasicNameValuePair("news_title", mNewsTitleET.getText().toString())); 
     pairs.add(new BasicNameValuePair("news_body", mNewsET.getText().toString())); 


     jsonObjectResult = jsonParser.makehttprequest(ADD_URL+"?user="+username+"", pairs); 

     if (jsonObjectResult == null) 
     { 
      error = "Error in the connection"; 
      return false; 
     } 

     try 
     { 
      if (jsonObjectResult.getInt("success") == 1) 
      { 
       error = jsonObjectResult.getString("message"); 
       return true; 
      } 
      else 
       error = jsonObjectResult.getString("message"); 

     } 
     catch (Exception ex) 
     { 

     } 

     return false; 
    } 

    @Override 
    protected void onPostExecute(Boolean aBoolean) 
    { 
     super.onPostExecute(aBoolean); 
     mProgressDialog.dismiss(); 
     Toast.makeText(mActivity.getApplicationContext(), error, Toast.LENGTH_LONG).show(); 
     dismiss(); 
    } 
} } 

在數據庫中,我選擇utf8。 並嘗試所有的解決方案沒有奏效。 請幫忙。

+0

嘗試在添加文件中添加一個靜態文本到數據庫中(不要從請求中讀取它),這樣你就可以將問題分成兩部分,你可以看到問題是從android客戶端到php服務器還是從php服務器到數據庫。 – Soosh

+0

請問,你可以編輯我的代碼在文件中增加 我希望你幫助我我失去了希望。 – yazan

回答

0

希望這些提示將幫助:

嘗試在另外的文件添加一個靜態文本到數據庫中(不從要求讀它),所以你可以把這個問題分成兩個部分,你可以看到,如果問題是來自Android客戶端到PHP服務器或PHP服務器的數據庫,如:

<?php 

/* 
    please type the Arabic string(salam) with your own keyboard and do not 
    copy my version because I used Persian keyboard. 
*/ 
$arabic_string = 'سلام'; 

file_put_contents("arabic_file", $arabic_string); 

$query_params = array(
    ':news_date' => $_POST['news_date'], 
    ':news_title' => $_POST['news_title'], 
    ':news_body' => $arabic_string 
); 

?> 

現在同時檢查「arabic_file」的內容,也是數據庫。

- 如果文件不好(包含亂碼),那麼問題來自你的PHP服務器(就像也許它不支持阿拉伯語言,...)。

- 如果數據庫是不正常(亂碼再次),那麼也許這個問題是您的數據庫或PHP的PDO是將數據插入到數據庫的方式,你應該指定編碼爲您的連接,如:

$connection_str = "mysql:host=$host;dbname=$db;charset=utf8"; 

- 如果一切正常,那麼問題是阿拉伯字符串來自你的Android設備的方式。

相關問題