Hey Guys I'm trying to make registration page using mysqli and php for backend and java for front end. My PHP Connection file works file even i've inserted data as well when i tried to insert data using android application it doesn't insert and even it doesn't show any error in log.
I'm Begginer in android. Please forgive if you found any error.
Here is my Insert File
require_once('dbConnect.php');
$email = $_POST['email'];
$password = $_POST['password'];
$full_name = $_POST['full_name'];
$registration_num = $_POST['registration_num'];
$contact_num = $_POST['contact_num'];
$sql = "INSERT INTO tblregistration (Email, Password,Full_name,Registration_number,Contact_number) VALUES ($email,$password,$full_name,$registration_num,$contact_num)";
if(mysqli_query($con,$sql)) {
echo "Successful";
}
mysqli_close($con);
Here is my Registration Page
b1 = (Button)findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
str_email = "a";
str_password = "a";
str_full_name = "a";
str_registration_num = "a";
str_contact_number = "a";
new InsertData().execute();
}
});
private class InsertData extends AsyncTask<String, Void, String> {
String JSON_STRING;
JSONObject jsonObject = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
}
@Override
protected String doInBackground(String... params) {
JSONParser rh = new JSONParser();
HashMap<String, String> data = new HashMap<>();
data.put("email", str_email);
data.put("password", str_password);
data.put("full_name", str_full_name);
data.put("registration_num", str_registration_num);
data.put("contact_num", str_contact_number);
JSON_STRING = rh.sendPostRequest(AppConfig.URL_REGISTER, data);
return JSON_STRING;
}
}
Here is my HttpURLConnection file
public String sendPostRequest(String requestURL,
HashMap<String, String> postDataParams) {
//Creating a URL
URL url;
//StringBuilder object to store the message retrieved from the server
StringBuilder sb = new StringBuilder();
try {
//Initializing Url
url = new URL(requestURL);
//Creating an httmlurl connection
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//Configuring connection properties
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
//Creating an output stream
OutputStream os = conn.getOutputStream();
//Writing parameters to the request
//We are using a method getPostDataString which is defined below
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode = conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
sb = new StringBuilder();
String response;
//Reading server response
while ((response = br.readLine()) != null){
sb.append(response);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for (Map.Entry<String, String> entry : params.entrySet()) {
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
return result.toString();
}