2012-02-11 19 views
0

我'創建在機器人簡單的應用程序,它通過PHP從表單數據發送到MySQL數據庫,並通過使用這裏函數httppost發佈數據是我在Java代碼從一個機器人應用MySQL數據庫

public class NouvelUtilisateur extends Activity 
{ 
    EditText username,password,name,lastname,phone,adresse ; 
    Button bouton ; 
    HttpPost httppost; 
    StringBuffer buffer; 
    HttpClient httpclient ; 



    @Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.nouveau); 

username = (EditText) findViewById(R.id.id1) ; 
password = (EditText) findViewById(R.id.pwd); 
name = (EditText) findViewById(R.id.name); 
lastname = (EditText) findViewById(R.id.Lastname); 
phone = (EditText) findViewById(R.id.phone); 
adresse = (EditText) findViewById(R.id.adress); 
bouton = (Button) findViewById(R.id.button3) ; 

final String id1 = username.getText().toString(); 
final String mdp = password.getText().toString(); 
final String prenom = name.getText().toString(); 
final String nom = lastname.getText().toString(); 
final String tel = phone.getText().toString(); 
final String adresse1 = adresse.getText().toString(); 




bouton.setOnClickListener(new View.OnClickListener() 
{ 
    public void onClick(View nouveau) 

    { 

        try { 
         httpclient = new DefaultHttpClient(); 
         httppost = new HttpPost("http://192.168.1.110/nouveau.php"); 
         ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>(); 
        postParameters.add(new BasicNameValuePair("Username", id1)); 
        postParameters.add(new BasicNameValuePair("Password", mdp)); 
        postParameters.add(new BasicNameValuePair("Name", prenom)); 
        postParameters.add(new BasicNameValuePair("Lastname", nom)); 
        postParameters.add(new BasicNameValuePair("Phone", tel)); 
        postParameters.add(new BasicNameValuePair("Adresse", adresse1)); 
        httppost.setEntity(new UrlEncodedFormEntity(postParameters));     
        HttpResponse response = httpclient.execute(httppost); 
        Log.i("postData", response.getStatusLine().toString()); 
         } 
         catch(Exception e) 
         { 
          Log.e("log_tag", "Error: "+e.toString()); 
         } 
        } 
       }); 
      } 
     } 

這裏是我的PHP代碼

<?php 
mysql_connect("localhost","root","") or die(mysql_error()); 
mysql_select_db("agenceimmo"); 
$username = $_POST['Username']; 
$password = $_POST['Password'] ; 
$nom = $_POST['Lastname'] ; 
$prenom = $_POST['Name'] ; 
$tel = $_POST['Phone'] ; 
$adresse = $_POST['Adresse'] ; 
$query_add="INSERT INTO clients (`id` ,`password` ,`nom` ,`prenom` ,`tel` ,`adresse`) 
VALUES ('.$username','.$password', '.$nom','.$prenom','.$tel','.$adresse')"; 
$query_exec=mysql_query($query_add) or die(mysql_error()); 
mysql_close() 
?> 

我真的需要你的幫助,請我看不到任何錯誤

+0

什麼錯誤,你好嗎,有關係嗎? – Ivo 2012-02-11 00:56:43

+0

HTTP/1.1 403 Forbidden – ziz194 2012-02-11 01:12:43

+0

您應驗證您是否可以從不同網頁上的腳本中發佈數據,並確保您的.htaccess文件不阻止訪問。這意味着您應該先檢查服務器,然後再擔心Android會成爲潛在的問題。 – Jakar 2012-02-11 02:48:36

回答

0

首先,檢查你有兩個文件夾和文件的權限。 其次,去你的根目錄下,請嘗試以下操作:

touch index.html 

(或index.php文件),然後重新啓動httpd的:

/etc/init.d/httpd restart 
+0

我如何檢查權限? – ziz194 2012-02-11 11:58:22

+0

@ ziz194這取決於您正在運行的操作系統。您的網絡服務器的進程需要訪問其根目錄中的文件。確保它可以正常提供html和php頁面。 – Ivo 2012-02-11 14:00:38

+0

是的,它可以正常服務的PHP和HTML,因爲我已經做了一個HTML格式和PHP頁面發送數據到數據庫的測試,所以是的,我認爲它可以爲PHP和HTML – ziz194 2012-02-11 18:31:28

相關問題