I am trying to insert a row into my msql database through wamp server. The database is on the localhost.
I am using android stuido as the ide and don't get any errors or warnings in the log. I couldn't find what's wrong with my code. I'm guessing that the problem is whether with httpclient or php files because new username and password is successfully passed on to NewProductActivty.java
. How to fix this issue?
Here is my java and php code:
functions.php
:
<?php
$DB_HOST = "localhost";
$DB_DATABASENAME = "kimnerede";
$DB_USERNAME = "root";
$DB_PASS = "";
define("BASARISIZ", "-1");
define("BASARILI", "1");
define("ARKADAS_BULUNAMADI_ERROR", "-2");
define("ARKADAS_ZATEN_MEVCUT_ERROR", "-3");
define("PROFIL_BULUNAMADI_ERROR", "-4");
function dbConnect() {
$db = mysqli_connect(DB_HOST, DB_DATABASENAME, DB_PASS, DB_USERNAME);
if (!$db)
return null;
mysqli_query($db, 'SET NAMES utf8');
return $db;
}
Other functions:
function arkadasEkle($db, $name, $pass) {
$sorgu = "INSERT INTO deneme123 (name, pass) VALUES ('$name', '$pass')";
if(!mysqli_query($db, $sorgu))
return false;
return true;
}
?>
addclient.php
:
<?php
//require_once('functions.php');
include('functions.php');
if((!isset($_POST['name']) || empty($_POST['name'])) &&
(!isset($_POST['pass']) || empty($_POST['pass'])))
die(BASARISIZ);
$name= $_POST['name'];
$pass= $_POST['pass'];
$db = dbConnect();
if(arkadasEkle($db, $name, $pass))
die(BASARILI);
die(BASARISIZ);
?>
These php files are in C:\wamp\www\kimnerede
directory.
This is the class where the connection and the insertion is made:
public class NewProductActivity extends AsyncTask<String,Void,String>{
public static final String KIM_NEREDE_BASE_URL = "http://10.0.2.2/kimnerede/";
public static final String KIM_NEREDE_PROFIL_KAYDET_URL = KIM_NEREDE_BASE_URL + "addclient.php";
private static final String TAG = "NetworkManager";
private Context context;
public NewProductActivity(Context context)
{
this.context = context;
}
public static String Ekle(String p_name, String p_pass)
{
BufferedReader in = null;
try {
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost(KIM_NEREDE_PROFIL_KAYDET_URL);
List<NameValuePair> parametreList = new ArrayList<NameValuePair>();
parametreList.add(new BasicNameValuePair("name", p_name));
parametreList.add(new BasicNameValuePair("pass", p_pass));
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
return in.readLine();
} catch (Exception e) {
Log.d(TAG, "Profil kaydedilirken hata olustu", e);
} finally {
if (in != null) {
try {
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return null;
}
@Override
protected String doInBackground(String... params) {
Ekle(params[0],params[1]);
return null;
}
}
This class is called by creating an object in LoginActivity.java
:
public void SignUpClicked(View view) {
new NewProductActivity(this).execute(UserName.getText().toString(),Password.getText().toString());
}