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


i doing login , register subsystem of mobile application, did following youtube tutorial video, register feature working data stored in database.

but when try login username created, shows message "username , password not exist!", doesn't return error message.i checked coding several times, googled answer couldn't find any, in advance! =)

**now i'm sure activity don't have problem, 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 user 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 login public boolean getuserloggedin() {     if(localdb.getboolean("loggedin",false))         return true;     else         return false; } //clear data when user log out public void cleardata(){     sharedpreferences.editor editor = localdb.edit();     editor.clear();     editor.commit(); } } 

here 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 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 * userdetails username= ? , 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);  ?> 

i can't understand code try new networking lib.("volley") calls. example login , registration.

//layout of activity <?xml version="1.0" encoding="utf-8"?> <relativelayout  xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="2dp" tools:context="com.test.test.screenone">   <edittext     android:layout_width="240dp"     android:layout_height="wrap_content"     android:id="@+id/etusername"     android:layout_margintop="150dp"     android:hint="username"     android:layout_alignparenttop="true"     android:layout_centerhorizontal="true" />  <edittext     android:layout_width="240dp"     android:layout_height="wrap_content"     android:id="@+id/etpassword"     android:hint="password"     android:layout_below="@+id/etusername"     android:layout_centerhorizontal="true" />  <button     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="login"     android:id="@+id/blogin"     android:layout_below="@+id/etpassword"     android:layout_centerhorizontal="true"     android:layout_margintop="50dp" />  <button     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="save"     android:id="@+id/bsave"     android:layout_below="@+id/blogin"     android:layout_centerhorizontal="true"     android:layout_margintop="42dp" /> </relativelayout>  

activity has 2 buttons , 2 edittext, login button login via server , save button saves data in server:

package com.test.test;  import android.app.progressdialog; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.view.window; import android.widget.button; import android.widget.edittext; import android.widget.toast;  import com.android.volley.authfailureerror; import com.android.volley.request; import com.android.volley.response; import com.android.volley.volleyerror; import com.android.volley.toolbox.stringrequest;  import java.util.hashmap; import java.util.map;  public class screenone extends appcompatactivity {  private static final string url_login = "http://ip_address/login.php"; private static final string url_save = "http://ip_address/save.php"; private edittext username; private edittext password; private button login; button save; string name; string pass;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.screen_one);      username = (edittext) findviewbyid(r.id.etusername);     password = (edittext) findviewbyid(r.id.etpassword);      (login = (button) findviewbyid(r.id.blogin)).setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             request();         }     });      (save = (button) findviewbyid(r.id.bsave)).setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             saverequest();         }     }); }  private void saverequest() {     name = username.gettext().tostring().trim();     pass = password.gettext().tostring().trim();     final progressdialog mdialog = new progressdialog(this);     mdialog.requestwindowfeature(window.feature_no_title);     mdialog.setmessage("loading...");     mdialog.show();      stringrequest request = new stringrequest(request.method.post, url_save,             new response.listener<string>() {                 @override                 public void onresponse(string response) {                     mdialog.dismiss();                     toast.maketext(screenone.this, response, toast.length_long).show();                     username.settext("");                     password.settext("");                 }             },             new response.errorlistener() {                 @override                 public void onerrorresponse(volleyerror error) {                     mdialog.dismiss();                     toast.maketext(screenone.this, "something went wrong", toast.length_long).show();                 }             }){         @override         protected map<string, string> getparams() throws authfailureerror {             map<string, string> key = new hashmap<>();             key.put("username", name);             key.put("password", pass);             return key;         }     };      networkcalls.getinstance().addtorequestqueue(request); }  private synchronized void request() {     name = username.gettext().tostring().trim();     pass = password.gettext().tostring().trim();     final progressdialog mdialog = new progressdialog(this);     mdialog.requestwindowfeature(window.feature_no_title);     mdialog.setmessage("loading...");     mdialog.show();      stringrequest request = new stringrequest(request.method.post, url_login,             new response.listener<string>() {                 @override                 public void onresponse(string response) {                     mdialog.dismiss();                     toast.maketext(screenone.this, response, toast.length_long).show();                     username.settext("");                     password.settext("");                 }             },             new response.errorlistener() {                 @override                 public void onerrorresponse(volleyerror error) {                     mdialog.dismiss();                     toast.maketext(screenone.this, "something went wrong", toast.length_long).show();                 }             }){         @override         protected map<string, string> getparams() throws authfailureerror {             map<string, string> key = new hashmap<>();             key.put("username", name);             key.put("password", pass);             return key;         }     };      networkcalls.getinstance().addtorequestqueue(request);  } } 

singleton class volley request:

import android.content.context;  import com.android.volley.request; import com.android.volley.requestqueue; import com.android.volley.toolbox.volley;  /**  * created w4r10ck on 12-09-2016.  */ public class networkcalls {     private requestqueue requestqueue;     private static context context;      private static networkcalls ourinstance = new networkcalls();      public static networkcalls getinstance() {         return ourinstance;     }      private networkcalls() {     }      public requestqueue getrequestqueue(){         requestqueue = volley.newrequestqueue(context.getapplicationcontext());         return requestqueue;     }      public <t> void addtorequestqueue(request<t> request){         getrequestqueue().add(request);     } } 

api call server:

 //conn.php connection (file one) <?php $host = "localhost"; $user = "root"; $pass = ""; $db_name = "hello";  $con = new mysqli($host,$user,$pass,$db_name);  if($con -> connect_error){ echo "connection error"; }      //save.php(file two) <?php $username = $_post['username']; $password = $_post['password']; require_once('conn.php');  $sql = "insert user (username, password) values ('$username','$password')";  if($con -> query($sql) === true) { echo "user added"; } //$con -> close(); ?> ?>  //login.php(file three) <?php require_once('conn.php');  $username = $_post['username']; $password = $_post['password'];  $sql = "select * user username = '$username' , password = '$password'";  $result = mysqli_query($con,$sql);  if(mysqli_fetch_array($result) == null){ echo "invalid cred."; }else{ echo "success"; }  $con->close(); ?> 

lastly create 1 db named hello , table in localhost user 2 fields username, password.


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -