This question already has an answer here:
I am new to PHP and mySQL databases. I am trying to build an android app for a university-project, that pulls data from a mySQL MAMP database.
I have a working connection to the database and i can successfully log in. Since the user-name is a unique key, i want to give it to another .php file, that pulls user specific data from another table of the database. For some reason $_SESSION does not work. Have been trying things for a few hours now - can't seem to fix it.
Looking forward to ANY help i can get. Thanks a lot :)
EDIT: If i manually type in the user-name instead of the variable $snr, it works as intended. Thats why i think it has something to do with $_SESSION in showimpfungen.php
UPDATE: So after i have done some troubleshooting - the problem seems to be the following. If i press the "login" button on my android emulator - a session file is created in which the variable is stored correctly. But after i run the showimpfungen.php script, another session file is created with a different ID. I guess thats the reason for Notice: Undefined index: why in /Applications/MAMP/htdocs/showimpfungen.php on line 11
. Also, i get PHP Notice: Undefined index: user_name in /Applications/MAMP/htdocs/login.php on line 7
and PHP Notice: Undefined index: password in /Applications/MAMP/htdocs/login.php on line 9
when running it in browser. My guess is, that i get those because they get their values from the user input in the emulator.
conn.php:
<?php
session_start();
$db_name = "Impfdatenbank";
$mysql_username = "root";
$mysql_password = "root";
$server_name = "localhost";
$conn = mysqli_connect($server_name, $mysql_username, $mysql_password, $db_name);
if(mysqli_connect_error($conn))
{
echo "Failed to connect";
}
?>
login.php:
<?php
session_start();
require "conn.php";
$user_name = $_POST["user_name"];
$_SESSION['why'] = $user_name;
$user_pass = $_POST["password"];
$mysql_qry = "select * from personen_daten where svnr like '$user_name' and password like '$user_pass';";
$result = mysqli_query($conn, $mysql_qry);
if(mysqli_num_rows($result) > 0){
echo "Login successful.";
}
else{
echo "Login failed.";
}
?>
showimpfungen.php:
<?php
session_start();
require "conn.php";
$snr = $_SESSION['why'];
$mysql_qry = "select * from Impfungen where svnr like '$snr'";
$query = mysqli_query($conn, $mysql_qry);
if($query)
{
while ($row = mysqli_fetch_array($query))
{
$flag[] =$row;
}
print(json_encode($flag));
}
mysqli_close($conn);
?>
</div>