Unable to fetch data from MySQL database using PHP in Android Studio

Question!

I was doing login and register subsystem of mobile application, I did it by following a youtube tutorial video, the register feature is working well as all the data were successfully stored in the database.

But when I try to login with the username that was already created, it shows a message "Username and password are not exist!", AS doesn't return any error message.I checked all the coding for several times, googled for the answer but I couldn't find any, Thanks in advance! =)

**Now I'm sure that my activity don't have any problem, but the php scripting file

LocalDb

public class LocalDb {
public static final String SP_Name = "UserInfo";
SharedPreferences localDb;

//Constructor
public LocalDb(Context context)
{
    localDb = context.getSharedPreferences(SP_Name,0);
}

//store user data
public void storeData(Contact contact)
{
    SharedPreferences.Editor editor = localDb.edit();
    editor.putString("Username",contact.username);
    editor.putString("Email",contact.email);
    editor.putString("Student_ID",contact.student_id);
    editor.putString("Password",contact.password);
    editor.commit();
}

//get information about which user is currently logged in
public Contact getLoggedInUser()
{
    String username=localDb.getString("Username","");
    String email=localDb.getString("Email","");
    String student_id=localDb.getString("Student_ID","");
    String password=localDb.getString("Password","");

    Contact storedContact=new Contact(username,email,student_id,password);
    return storedContact;
}

public void setUserLoggedIn(boolean loggedIn)
{
    SharedPreferences.Editor editor=localDb.edit();
    editor.putBoolean("loggedIn",loggedIn);
    editor.commit();
}

//Tell user whether is currently login
public Boolean getUserLoggedIn()
{
    if(localDb.getBoolean("loggedIn",false))
        return true;
    else
        return false;
}
//Clear data when a user log out
public void clearData(){
    SharedPreferences.Editor editor = localDb.edit();
    editor.clear();
    editor.commit();
}
}

Here are the php files:

Register.php

<?php
$con = mysqli_connect("localhost" , "my_db_name" , "my_password" , "my_db");

$username = $_POST["Username"];
$email = $_POST["Email"];
$student_id = $_POST["Student_ID"];
$password = $_POST["Password"];

$insertquery = mysqli_prepare($con , "INSERT INTO UserDetails (Username , Email , Student_ID , Password ) VALUES (? , ? , ? , ?)");
mysqli_stmt_bind_param($insertquery , "ssss" , $username , $email , $student_id , $password);
mysqli_stmt_execute($insertquery);

mysqli_stmt_close($insertquery);

mysqli_close($con);

?>

getUserData.php

<?php
$con = mysqli_connect("localhost" , "my_db_name" , "my_password" , "my_db");

$username = $_POST["Username"];
$password = $_POST["Password"];

$selectquery = mysqli_prepare($con , "SELECT * FROM UserDetails WHERE Username= ? AND Password= ?");
mysqli_stmt_bind_param($selectquery , "ss" , $username , $password);
mysqli_stmt_execute($selectquery);

mysqli_stmt_store_result($selectquery);
mysqli_stmt_bind_result($selectquery , $username , $email , $student_id , $password);

$user=array();

while(mysqli_stmt_fetch($selectquery);  
(
    $user[username] = $username;
    $user[email] = $email;
    $user[student_id] = $student_id;
    Suser[password] = $password;
)
echo json_encode($user);

mysqli_stmt_close($selectquery);
mysqli_close($con);

?>
By : CZen Lau


Answers

I certainly can't understand your code but try new Networking Lib.("Volley") for calls. I example for Login and Registration.

//Layout of Activity
By : W4R10CK


This video can help you solving your question :)
By: admin