将数据从Android应用程序发布到MySql数据库

时间:2012-02-11 00:53:09

标签: php android mysql

我在android中创建一个简单的应用程序,它通过php将表单中的数据发送到mysql数据库,并使用httppost函数,这是我在java中的代码

 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()
?>

我真的需要你的帮助,我看不到任何错误

1 个答案:

答案 0 :(得分:0)

首先,检查文件夹和文件的权限。 其次,转到根目录并尝试以下操作:

touch index.html

(或index.php),然后重启httpd:

/etc/init.d/httpd restart