0

I am new to Android Programming.I am trying to build an Android Project which is connected to an online MySQL Server using PHP to pass the data from Android device to the online Server.I have two tables one of which contains the login details of the employee i.e.EMPLOYEE_DATA, and the 2nd table contains the respective employee details i.e.EMPLOYEE_DETAILS.

So, I have 2 PHP files : the 1st PHP script is for the Login of the employee which runs when the employee clicks on the Login Button.This script needs to get the username of the employee which is logged in and pass it to the second PHP file.The 2nd PHP script is used for inserting the details of the respective employees. Now I need to pass the username from the 1st PHP scriptto the 2nd PHP script and insert that username to the "USERNAME" column of the table "EMPLOYEE_DETAILS". Can anyone please suggest how to proceed. I have tried using $_SESSIONS but am not getting the desired result. I have been stuck with this for a long time. Any help would be appreciated.

Edit 1 :

Login.php

    <?php

$_SESSION['user_mobile'] =  $_POST["mobile_num"];
$_SESSION['user_pass'] = $_POST["password"];
require "conn.php";

$user_mobile='';
 $user_pass='';

$user_mobile = $_SESSION['user_mobile'];
$user_pass = $_SESSION['user_pass'];

$mysql_qry = "select * from employee_data where mobile like '$user_mobile' and password like '$user_pass';";
$result = mysqli_query($conn,$mysql_qry);
if(mysqli_num_rows($result) > 0 ) {
    $row = mysqli_fetch_assoc($result);
    $_POST['user'] = $name;
    $name= $row["name"];
    echo "Login successful.<br /> Welcome " ;
        echo $name; 
        $insertName = "INSERT INTO employee_details(name) VALUES ('$name');";
        $resultName = mysqli_query($conn,$insertName);
        $shift = "SELECT CASE WHEN CURTIME() < 7 THEN 'Morning'
                              WHEN CURTIME() < 12 THEN 'Afternoon'
                              WHEN CURTIME() <17 THEN 'Evening'
                              ELSE 'Night'
                              END;";

        $insertShift = "insert into employee_details(shift) values ('$shift');";
        $resultShift = mysqli_query($conn,$insertShift);  
} 
else{
echo "Login failed.";   
} 
?>

I want to pass the $name variable to another PHP Script which will be used to insert the details of the user which is currently logged in.So, the username of the Employee will come from the $name variable and the other details will be the input from the Android device.

Edit 2:

Insert.php

<?php
require "conn.php";
include "login.php";
echo $_POST['user'];
$enquiry = $_POST["enquiry"];
$retail = $_POST["retail"];
$collection = $_POST["collection"];
$booking = $_POST["booking"];
$evaluation = $_POST["evaluation"];
$test_drive = $_POST["test_drive"];
$home_visit = $_POST["home_visit"];


date_default_timezone_set('Asia/Kolkata');
$IST = date('d-m-Y H:i');


$mysql_qry1 = "INSERT INTO employee_details(enquiry,retail, 
collection,booking, evaluation, test_drive, home_visit, date, time) values ('$enquiry','$retail','$collection','$booking','$evaluation','$test_drive',
'$home_visit',CURDATE(),CURTIME());";

$shift = "SELECT   TIME(CURRENT_TIME),
         CASE WHEN TIME(CURRENT_TIME) BETWEEN '01:00:00' AND '07:00:00' THEN 'Morning'
              WHEN TIME(CURRENT_TIME) BETWEEN '08:00:00' AND '13:00:00' THEN 'Afternoon'
              WHEN TIME(CURRENT_TIME) BETWEEN '13:00:00' AND '18:00:00' THEN 'Evening'
         END;";

$mysql_qry2 = "INSERT INTO employee_details(shift) values ('$shift');";
$ins = mysqli_query($conn,$mysql_qry2);
if($conn->query($mysql_qry1) === TRUE) 
    echo "Your details has been successfully inserted.";

else 
    echo "Error: " .$mysql_qry1. "<br>" . $conn->error;


$conn->close();
?>

I want to pass the $name variable from "Login.php" to "Insert.php" and insert into the "USERNAME" column in employee_details.

Pranami
  • 27
  • 1
  • 10
  • 1
    show us what you have done. without code we can't help you. also your question is not clear enough. do you asking for connectivity between android and mysql through php? – Abhishek Singh Jan 31 '17 at 06:44
  • @AbhishekSingh I have the attached the PHP code. Can you please check and let me know how to proceed. Actually I am not able the $name variable to the next PHP file which is used to insert the details into the Database of the respective users. – Pranami Jan 31 '17 at 06:59
  • why tagged this question to android – Abhishek Singh Jan 31 '17 at 07:02
  • in another php just make a method and pass the parameter from here...inclue that php in this php – Abhishek Singh Jan 31 '17 at 07:04
  • @AbhishekSingh I want to pass the $name variable in the "insert.php". Once the user clicks on the "Log In" button in the Android device, the "Login.php" script runs. And once the user the logged in, and inserts the inputs and clicks the "Submit" Button, the "insert.php" script is called. So, I wanted the $name to be inserted into "EMPLOYEE_DETAILS" table when the "Submit" button is clicked. So, do I need to make a method in "insert.php" and pass the $name from login.php.IS that what you are trying to say? – Pranami Jan 31 '17 at 07:12
  • ohk i understand your problem... ` echo "Login successful.
    Welcome " .$name;` its a response that android gets after calling login.php ...store this response in some variable of shared preferance... split name from it,,,, and post this variable when user click the submit
    – Abhishek Singh Jan 31 '17 at 07:20
  • @AbhishekSingh I am using an AsyncTask to show the echo "Login successful.
    Welcome " .$name; The whole string is passed as a parameter in onPostExecute() method.Can you please tell me how to split the $name. I am not able to figure out how to get only the $name variable from the PHP script and use it in the Shared Peference.
    – Pranami Jan 31 '17 at 07:35
  • use a delemiter in string such as.. `echo "Login successful.
    Welcome ______" .$name;` then in android String[] str=youresponse.split("_____");..... name were contained in str[1]....
    – Abhishek Singh Jan 31 '17 at 09:14
  • @AbhishekSingh Thank you so much for your help. I solved the issue using your solution and its working now.Thanks once again :) – Pranami Feb 01 '17 at 11:52

1 Answers1

0

you are defining $_POST['user'] = $name before the declare $name variable so try this
login.php

<?php
require "conn.php";

$user_mobile= $_POST["mobile_num"];
$user_pass = $_POST["password"];;


$mysql_qry = "SELECT * FROM employee_data WHERE mobile = '$user_mobile' AND password ='$user_pass'";
$result = mysqli_query($conn,$mysql_qry);
if(mysqli_num_rows($result) > 0 ) {
    $row = mysqli_fetch_assoc($result);
    $name= $row["name"];
    $_POST['user'] = $name;
    echo "Login successful.<br /> Welcome",$name ;
    // Your rest of code

?>

Just try to show echo $_POST['user'] in in insert.php .

for better understandig please see this PHP Pass variable to next page

Suggestion please use prepare statement instead of direct mysqli_query function. Why are you using like in SQL query for matching password instead of = for login to user.

Community
  • 1
  • 1
gaurav
  • 1,281
  • 1
  • 13
  • 25
  • What I wanted was to Login using the mobile number and password and the username respective to that user will get displayed when the user logs in by getting it from the Database. And I want to pass that username to the next PHP Script to insert the details of that particular user along with the username. – Pranami Jan 31 '17 at 07:45
  • than you can try to store Session when user click login page start session and store `$_SESSION['username'] = $name` which is on `login.php` than get this session varable on `insert.php` . – gaurav Jan 31 '17 at 07:50
  • I tried using this approach.The problem was if I declared a $_SESSION['some_variable'] at the beginning of the PHP Script and call it in insert.php , then I am able to access it. But ,if I declare a $_SESSION['username'] = $name as you mentioned after the echo "Login successful.
    Welcome " .$name; statement and try calling this session variable in Insert.php, this value isn't getiing called.
    – Pranami Jan 31 '17 at 07:56
  • `echo "Login Succesful " ` statement is showing `$name` value when you click on login . – gaurav Jan 31 '17 at 08:10
  • Yeah.The echo statement shows the $name variable and I tried storing the $name variable in a $_SESSION['username'] = $name. Then I call the $_SESSION['username'] in insert.php file. But it doesnot return the $name value in Insert.php ,but it works fine in Login.php to show the name of the user. – Pranami Jan 31 '17 at 08:14
  • now try store like this `$_POST['user'] = $name ` in `login.php` & show in `insert.php` like `$_POST['user']` . – gaurav Jan 31 '17 at 08:25
  • Its not working. I get errors with Undefined Index. This is what I did: – Pranami Jan 31 '17 at 09:00
  • In Insert.php require "conn.php"; include "login.php"; echo $_POST['user']; and put $_POST['user'] in login.php as you mentioned. But its not working. And also I am getting errors with Undefined Index in Mobile_Num and password since I used include "login.php" in "insert.php" – Pranami Jan 31 '17 at 09:02
  • where you define `$_POST['user']` in login file – gaurav Jan 31 '17 at 09:03
  • we are doing mistake which `mysqli_fetch_assoc()` return a array with key name as column name than you should try to store like `$_POST['user'] = $row['name']` in login file in `if` clause try to show in `insert.php` – gaurav Jan 31 '17 at 09:11
  • After declaring the $name variable. Here is the code: – Pranami Jan 31 '17 at 09:11
  • $name= $row["name"]; echo "Login successful.
    Welcome " ; echo $name; $insertName = "INSERT INTO employee_details(name) VALUES ('$name');"; $_POST['user'] = $name;
    – Pranami Jan 31 '17 at 09:11
  • `$name = $row['name']` why is this not in above code. in `login.php` there is not `INSERT` query so please update your code for better understanding – gaurav Jan 31 '17 at 09:15
  • I solved the issue using the solution provided by Abhishek Singh.Thanks a lot for taking the time to look into my problem and providing the solution. :) – Pranami Feb 01 '17 at 11:56