I have created a login page in Android. The first user will register and then he will redirect to the login page where he has to enter these details as name and id. Plus this name & id will get display on another android activity. The issue is user registration is doing correctly. but while retriving this name & id it gives me error"{"status":"false","message":"Error occured, please try again!"}" Please let me know if I have done something wrong. I am using this with an android file. where these name & id will get displayed.
Code:
abc.php
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
// echo $_SERVER["DOCUMENT_ROOT"]; // /home1/demonuts/public_html
//including the database connection file
include_once("dbConfig.php");
$mi_id = isset($_POST['mi_id']) ? $_POST['mi_id'] : null;
$name = isset($_POST['name']) ? $_POST['name'] : null;
if( $mi_id == '' || $name == '' ){
echo json_encode(array( "status" => "false","message" => "Parameter missing!") );
}else{
$query= "SELECT * FROM registerdemo WHERE mi_id='$mi_id' AND name='$name'";
$result= mysqli_query($con, $query);
if(mysqli_num_rows($result) > 0){
$query= "SELECT * FROM registerdemo WHERE mi_id='$mi_id' AND name='$name'";
$result= mysqli_query($con, $query);
$emparray = array();
if(mysqli_num_rows($result) > 0){
while ($row = mysqli_fetch_assoc($result)!=NULL) {
$emparray[] = $row;
}
}
echo json_encode(array( "status" => "true","message" => "Login successfully!", "data" => $emparray) );
}else{
echo json_encode(array( "status" => "false","message" => "Invalid username or password!") );
}
mysqli_close($con);
}
}
else
{
echo json_encode(array( "status" => "false","message" => "Error occured, please try again!") );
}
?>
Java Code Welcome.java package com.exampledemo.sample.registerloginsession;
import android.content.Intent;
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class WelcomeActivity extends AppCompatActivity {
private TextView tvname,tvmi_id;
private Button btnlogout;
private PreferenceHelper preferenceHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
preferenceHelper = new PreferenceHelper(this);
tvmi_id = (TextView) findViewById(R.id.tvmi_id);
tvname = (TextView) findViewById(R.id.tvname);
btnlogout = (Button) findViewById(R.id.btn);
tvname.setText(preferenceHelper.getName());
tvmi_id.setText(getResources().getString(R.string.welcome_message, preferenceHelper.getMi_id()));
btnlogout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
preferenceHelper.putIsLogin(false);
Intent intent = new Intent(WelcomeActivity.this,See_Feedback.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
WelcomeActivity.this.finish();
}
});
}
}
Login.java
package com.exampledemo.sample.registerloginsession;
import android.content.Intent;
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class WelcomeActivity extends AppCompatActivity {
private TextView tvname,tvmi_id;
private Button btnlogout;
private PreferenceHelper preferenceHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
preferenceHelper = new PreferenceHelper(this);
tvmi_id = (TextView) findViewById(R.id.tvmi_id);
tvname = (TextView) findViewById(R.id.tvname);
btnlogout = (Button) findViewById(R.id.btn);
tvname.setText(preferenceHelper.getName());
tvmi_id.setText(getResources().getString(R.string.welcome_message, preferenceHelper.getMi_id()));
btnlogout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
preferenceHelper.putIsLogin(false);
Intent intent = new Intent(WelcomeActivity.this,See_Feedback.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
WelcomeActivity.this.finish();
}
});
}
}
Your code was poorly written; I tried to fix it.
Please note that I strongly recommends the use of prepared statement to protect you against MySQL Injection
Try this code below:
<?php
//Don't run code until POST request is received
if($_SERVER['REQUEST_METHOD']=='POST'){
include_once("dbConfig.php");
//Extract content of POST request into variables $mi_id, $name
$mi_id = isset($_POST['mi_id']) ? $_POST['mi_id'] : null;
$name = isset($_POST['name']) ? $_POST['name'] : null;
//Don't run next code if any of these variables
//is empty null or false; return message
if( empty( $mi_id ) || empty( $name ) ){
echo json_encode(array( "status" => "false","message" => "Parameter missing!") );
}
//If all you made it here, let's run the query;
$query= "SELECT * FROM registerdemo WHERE mi_id='$mi_id' AND name='$name'";
//Only if there's result and greater than 0, then return something
if( $result= mysqli_query($con, $query)) {
if(mysqli_num_rows($result) > 0){
$emparray = array();
while ($row = mysqli_fetch_assoc($result)!=NULL) {
$emparray[] = $row;
}
echo json_encode(array( "status" => "true","message" => "Login successfully!", "data" => $emparray) );
}
else{
echo json_encode(array( "status" => "false","message" => "Invalid username or password!") );
}
}
else {
echo json_encode(array( "status" => "false","message" => "Error occured, please try again!") );
}
// Close mysql connection;
mysqli_close($con);
}
?>
Hope this helps.