Android Database connectivity

  • Replies:1
  • Answered
Linu S.
  • Forum posts: 77

Oct 4, 2016, 9:18:04 AM via Website

I can't save my data to mysql database

Here is my full code

MainActivity.java

package com.example.administrator.regis;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

private EditText editTextName;
private EditText editTextUsername;
private EditText editTextPassword;
private EditText editTextEmail;

private Button buttonRegister;

private static final String REGISTER_URL = "http://localhost:443/Android/register.php";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    editTextName = (EditText) findViewById(R.id.editTextName);
    editTextUsername = (EditText) findViewById(R.id.editTextUserName);
    editTextPassword = (EditText) findViewById(R.id.editTextPassword);
    editTextEmail = (EditText) findViewById(R.id.editTextEmail);

    buttonRegister = (Button) findViewById(R.id.buttonRegister);

    buttonRegister.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    if(v == buttonRegister){
        registerUser();
    }
}

private void registerUser() {
    String name = editTextName.getText().toString().trim().toLowerCase();
    String username = editTextUsername.getText().toString().trim().toLowerCase();
    String password = editTextPassword.getText().toString().trim().toLowerCase();
    String email = editTextEmail.getText().toString().trim().toLowerCase();

    register(name,username,password,email);
}

private void register(String name, String username, String password, String email) {
    String urlSuffix = "?name="+name+"&username="+username+"&password="+password+"&email="+email;
    class RegisterUser extends AsyncTask<String, Void, String>{

        ProgressDialog loading;


        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loading = ProgressDialog.show(MainActivity.this, "Please Wait",null, true, true);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            loading.dismiss();
            Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();
        }

        @Override
        protected String doInBackground(String... params) {
            String s = params[0];
            BufferedReader bufferedReader = null;
            try {
                URL url = new URL(REGISTER_URL+s);
                HttpURLConnection con = (HttpURLConnection) url.openConnection();
                bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));

                String result;

                result = bufferedReader.readLine();

                return result;
            }catch(Exception e){
                return null;
            }
        }
    }

    RegisterUser ru = new RegisterUser();
    ru.execute(urlSuffix);
}

}

context_main.xml

<?xml version="1.0" encoding="utf-8"?>

xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:orientation="vertical"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/string_enter_name"
    android:layout_marginTop="80dp"/>

<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/editTextName" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/string_choose_username" />

<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/editTextUserName" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/string_choose_password" />

<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="textPassword"
    android:id="@+id/editTextPassword" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/string_enter_email" />

<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="textEmailAddress"
    android:id="@+id/editTextEmail" />

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/button_register"
    android:id="@+id/buttonRegister" />

and my php codes are

dbConnect.php

<?php
define('HOST','localhost');
define('USER','root');
define('PASS','');
define('DB','Android');

$con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');
?>

register.php

<?php

    $name = $_GET['name'];
    $username = $_GET['username'];
    $password = $_GET['password'];
    $email = $_GET['email'];

    if($name == '' || $username == '' || $password == '' || $email == ''){
        echo 'please fill all values';
    }else{
        require_once('dbConnect.php');
        $sql = "SELECT * FROM users WHERE username='$username' OR email='$email'";

        $check = mysqli_fetch_array(mysqli_query($con,$sql));

        if(isset($check)){
            echo 'username or email already exist';
        }else{              
            $sql = "INSERT INTO users (name,username,password,email) VALUES('$name','$username','$password','$email')";
            if(mysqli_query($con,$sql)){
                echo 'successfully registered';
            }else{
                echo 'oops! Please try again!';
            }
        }
        mysqli_close($con);
    }
    ?>

Help... Urgent..

Reply