dimanche 26 juin 2016

Why can't a MySQL database, connected to my android app, be updated?


Here is the PHP file codes to connect to the MySQL database testing and the Java code to pass the data to PHP. Please help me find the error. It won't update the database when I run it. However, I can use PHP to update the database and it will show.

PHP file:

<?php

 $db_name = "testing";
 $mysql_user = "root";
 $mysql_pass = "squidoo";
 $server_name = "localhost";
 $con = mysqli_connect($server_name,$mysql_user,$mysql_pass,$db_name)
 ?>

<?php
 require "dbconfigure.php";

 $firstname = isset($_POST["Firstname"])? $_POST["Firstname"]:null;
 $lastname = isset($_POST["Lastname"])? $_POST["Lastname"]:null;
 $birthdate = isset ($_POST["Birthdate"])? $_POST["Birthdate"]:null;
 $email = isset($_POST["email"])? $_POST["email"]: null;
 $username =  isset($_POST['username'])? $_POST['username'] : null;
 $password = isset ($_POST["password"])? $_POST['username'] : null;
 $sql_query = "INSERT INTO register (firstname, lastname, birthdate,        
 email,username,password)  
  VALUES('$firstname','$lastname','$birthdate','$email','$username','$password');";

?>

JAVA file:

package com.example.sony.testing;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class RegisterActivity extends Activity {

String firstname, lastname, birthdate, email, username, password;

EditText FirstName, LastName, Email, BirthDate, NewUsername, NewPassword;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.register_activity);



    FirstName = (EditText) findViewById(R.id.FirstName);
    LastName = (EditText) findViewById(R.id.LastName);
    BirthDate = (EditText) findViewById(R.id.BirthDate);
    Email = (EditText) findViewById(R.id.Email);
    NewUsername = (EditText) findViewById(R.id.NewUsername);
    NewPassword = (EditText) findViewById(R.id.NewPassword);


}

public void userReg(View view) {

    firstname = FirstName.getText().toString();
    lastname = LastName.getText().toString();
    birthdate = BirthDate.getText().toString();
    email = Email.getText().toString();
    username = NewUsername.getText().toString();
    password = NewPassword.getText().toString();

    String method = "register";
    BackgroundTask backgroundTask = new BackgroundTask(this);
    backgroundTask.execute(method, firstname, lastname, birthdate, email, username, password);
    finish();

      }


 }

package com.example.sony.testing;

      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 BackgroundTask extends AsyncTask <String,Void,String> {
AlertDialog alertDialog;
Context ctx;
  public BackgroundTask(Context ctx)
{

    this.ctx =ctx;
}


@Override
protected void onPreExecute() {
    alertDialog = new AlertDialog.Builder(ctx).create();
    alertDialog.setTitle("Login Information");
}
@Override
protected String doInBackground(String... params) {
    String reg_url = "http://10.0.2.2/webapp/register.php";
    String login_url = "http://10.0.2.2/webapp/login.php";
    String method = params[0];


    if (method.equals("register")) {
        String Firstname = params[1];
        String Lastname = params[2];
        String Birthdate = params[3];
        String email= params[4];
        String username = params[5];
        String password = params[6];

        try {
            URL url = new URL(reg_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            //httpURLConnection.setDoInput(true);

            OutputStream OS = httpURLConnection.getOutputStream();

            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));

            String data = URLEncoder.encode("Firstname","UTF-8")+"="+URLEncoder.encode(Firstname,"UTF-8")+"&"+
                    URLEncoder.encode("Lastname","UTF-8")+"="+URLEncoder.encode(Lastname,"UTF-8")+"&"+
                    URLEncoder.encode("Birthdate","UTF-8")+"="+URLEncoder.encode(Birthdate,"UTF-8")+"&"+
                    URLEncoder.encode("email","UTF-8")+"="+URLEncoder.encode(email,"UTF-8")+"&"+
                    URLEncoder.encode("username","UTF-8")+"="+URLEncoder.encode(username,"UTF-8")+"&"+
                    URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");

            bufferedWriter.write(data);
            bufferedWriter.flush();
            bufferedWriter.close();
            OS.close();
            InputStream IS = httpURLConnection.getInputStream();
            IS.close();
            httpURLConnection.connect();
            httpURLConnection.disconnect();
            return "Registration Success...";
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    else if(method.equals("login"))
    {
        String login_name = params[1];
        String login_pass = params[2];
        try {
            URL url = new URL(login_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            OutputStream outputStream = httpURLConnection.getOutputStream();

            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));

            String data = URLEncoder.encode("login_name","UTF-8")+"="+URLEncoder.encode(login_name,"UTF-8")+"&"+
                    URLEncoder.encode("login_pass","UTF-8")+"="+URLEncoder.encode(login_pass,"UTF-8");
            bufferedWriter.write(data);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
            String response = "";
            String line = "";

            while ((line = bufferedReader.readLine())!=null)
            {
                response+= line;
            }
            bufferedReader.close();
            inputStream.close();
            httpURLConnection.disconnect();

            return response;

        } 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();
    }
    }
  }

Aucun commentaire:

Enregistrer un commentaire