I wanted to insert data in mysql and also to retrieve, modify and delete rows in it. I don't have any programming knowledge, just a little database manipulation. This android studio is a whole new thing for me and its like i'm having problems from the tutorials regarding on connecting android studio to mysql. I tried to use the ip add of my computer, the 10.0.2.2 , the 127.0.0.1 and even registering for a free web hosting but it still doesn't insert data.
Another thing the dialog box that should display the status of registration doesn't work.
One last thing I know that the mistake is in this code because when i looked on their tutorial it worked but i can't figure it out why it doesn't work on me.
I really appreciate any help you can provide.
here's the php file
conn.php
<?php
$db_name = "dbapp";
$mysql_username="root";
$mysql_password="";
$server_name="localhost";
$con = mysqli_connect($server_name,$mysql_username,$mysql_password,$db_name);
?>
register.php
<?php
require "conn.php";
$f_name=$_POST["first_name"];
$m_name=$_POST["middle_name"];
$l_name=$_POST["last_name"];
$email_add=$_POST["emailadd"];
$user_name=$_POST["username"];
$user_pass=$_POST["userpass"];
$sql_query = "insert into tbluser (f_name, m_name, l_name, user_emadd, user_name, user_pass) values('$fname','$mname','$lname','email_add','$user_name','user_pass')";
?>
here's some of the android studio files
Register.java
package com.example.*****.myapplication;
// Above code is not really asterisk, i just wanna hide it..
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class Register extends Activity{
EditText ET_FNAME,ET_MNAME,ET_LNAME,ET_EMAIL,ET_USERNAME,ET_PASS;
String txtfname, txtmname, txtlname, txtusername, txtemail, txtpass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
ET_FNAME = (EditText)findViewById(R.id.et_fname);
ET_MNAME = (EditText)findViewById(R.id.et_mname);
ET_LNAME = (EditText)findViewById(R.id.et_lname);
ET_EMAIL = (EditText)findViewById(R.id.et_email);
ET_USERNAME = (EditText)findViewById(R.id.et_username);
ET_PASS = (EditText)findViewById(R.id.etpassword);
}
public void userReg(View view){
txtfname = ET_FNAME.getText().toString();
txtmname = ET_MNAME.getText().toString();
txtlname = ET_LNAME.getText().toString();
txtemail = ET_EMAIL.getText().toString();
txtusername = ET_USERNAME.getText().toString();
txtpass = ET_PASS.getText().toString();
String method = "register";
BackgroundWorker backgroundWorker = new BackgroundWorker(this);
backgroundWorker.execute(method,txtfname,txtmname, txtlname, txtemail, txtusername,txtpass);
finish();
}
}
here's another java class BackgroundWorker.java
package com.example.*****.myapplication;
import android.app.AlertDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
public class BackgroundWorker extends AsyncTask<String,Void,String> {
AlertDialog alertDialog;
Context ctx;
BackgroundWorker(Context ctx) {
this.ctx = ctx;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
String reg_url = "http://10.0.2.2/myapp/register.php";
String login_url = "http://10.0.2.2/myapp/login.php";
String method = params[0];
if (method.equals("register")) {
String fname = params[1];
String mname = params[2];
String lname = params[3];
String emailadd = params[4];
String username = params[5];
String userpass = params[6];
try {
URL url = new URL(reg_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
OutputStream OS = httpURLConnection.getOutputStream();
BufferedWriter bufferedwriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
String data = URLEncoder.encode("f_name", "UTF-8") + "=" + URLEncoder.encode(fname, "UTF-8") + "&" +
URLEncoder.encode("m_name", "UTF-8") + "=" + URLEncoder.encode(mname, "UTF-8") + "&" +
URLEncoder.encode("l_name", "UTF-8") + "=" + URLEncoder.encode(lname, "UTF-8") + "&" +
URLEncoder.encode("email_add", "UTF-8") + "=" + URLEncoder.encode(emailadd, "UTF-8") + "&" +
URLEncoder.encode("user_name", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8") + "&" +
URLEncoder.encode("user_pass", "UTF-8") + "=" + URLEncoder.encode(userpass, "UTF-8");
bufferedwriter.write(data);
bufferedwriter.flush();
bufferedwriter.close();
OS.close();
InputStream IS = httpURLConnection.getInputStream();
IS.close();
httpURLConnection.disconnect();
return "Registration Success";
} catch (MalformedURLException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate (Void...values){
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
if(result.equals("Registration Success"))
{
Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
}
else
{
alertDialog.setMessage(result);
alertDialog.show();
}
}
}
here are the column name in mysql, it has userid as primary key and autocrement.
fname
mname
lname
email_add
user_name
user_pass