Hello. I'm a newbie so sorry if this isn't the best forum to post my problem.
I am using a MySQL and PHP to create a web app. I have authentication, and I can register users. I also have a form that users provide information and it is successfully inserting data into a table in my database.
I will use fictional fields for my database table called meal_info:
username
dateStartedDiet
numberMealsPerDay
costPerMeal
Problem: Select user-specific data from the MySQL database, using Session username to select only the current user's data, then display it and do some calculations.
Here is thecode at the top, and I am fairly sure it's working:
session_start(); //execute commone code
require("common.php"); //includes code to connect to database, etc.
if(empty($_SESSION['user']))
{
// If they are not, we redirect them to the login page.
header("Location: login.php");
// Remember that this die statement is absolutely critical. Without it,
// people can view your members-only content without logging in.
die("Redirecting to login.php");
}
Here is the part of the code that has to do with displaying user data:
$userID = $_SESSION['user']['username']; //create a variable that is the session username which is identical to the field in our MySQL table
$query = "SELECT * FROM meal_info WHERE username = $userID"; //our SELECT statement
$result = db->query($query); //execute the query
$row_count = $result->num_rows;//count the rows in the table and place in variable to use in incremental loop code
for ($i = 0; $i < $row_count; $i++) : $row = $result->fetch_assoc(); //for each row in the table, fetch and create and array
$dateStart = $row['dateStartedDiet'];
$numberMeals = $row['numberMealsPerDay'];
$costMeal = $row['costPerMeal'];
echo $dateStart;
echo $numberMeals;
echo $costMeal;