我'創建在機器人簡單的應用程序,它通過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()
?>
我真的需要你的幫助,請我看不到任何錯誤
什麼錯誤,你好嗎,有關係嗎? – Ivo 2012-02-11 00:56:43
HTTP/1.1 403 Forbidden – ziz194 2012-02-11 01:12:43
您應驗證您是否可以從不同網頁上的腳本中發佈數據,並確保您的.htaccess文件不阻止訪問。這意味着您應該先檢查服務器,然後再擔心Android會成爲潛在的問題。 – Jakar 2012-02-11 02:48:36