Ajouter dans une Base de Donnée Mysql
Je souhaite réaliser une application permet d'ajouter des informations a partir d'une formulaire Android dans une base de donnée Mysql.
Comme un test ,j'ai réalisé une petite application envoie le nom et le prénom récuperés d'une EditText vers un table de ma base.
Le code de mon Activity:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
public class AjoutProduits extends Activity{
EditText Nom,Prenom ;
Button bouton ;
HttpPost httppost;
StringBuffer buffer;
HttpClient httpclient ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ajoutproduits);
Nom = (EditText) findViewById(R.id.editTextAjouter1) ;
Prenom = (EditText) findViewById(R.id.editTextAjouter2);
bouton = (Button) findViewById(R.id.buttonAjouter) ;
final String N = Nom.getText().toString();
final String P = Prenom.getText().toString();
bouton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View nouveau)
{
httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://10.0.2.2/etatdevente/ajouter.php");
try {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("Nom", N));
nameValuePairs.add(new BasicNameValuePair("Prenom", P));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
// HttpEntity entity = response.getEntity();
//InputStream is = entity.getContent();
Log.i("postData", response.getStatusLine().toString());
}
catch(Exception e)
{
Log.e("log_tag", "Error: "+e.toString());
}
}
});
}
} |
ajouter.php:
Code:
1 2 3 4 5 6 7 8 9 10 11 12
|
<?php
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("etatdevente");
$Nom = $_POST['Nom'];
$Prenom = $_POST['Prenom'] ;
$query_add="INSERT INTO nomprenom (`Nom` ,`Prenom` )
VALUES ('.$Nom','.$Prenom')";
$query_exec=mysql_query($query_add) or die(mysql_error());
mysql_close()
?> |
Le probleme que j'ai, est que les donnée String sont enregistrés dans ma base sous forme d'une point, et des 0 pour les données de type Int ou date.???
Je ne sais pas ou j'ai commis l'erreur, mais Logcat ne m'affiche rien.
Merci :)