This question already has an answer here:
I'm coding a webpage that has users register and then login. When trying to display the user's information on another page, I am struggling with trying to figure out why only the "username" variable will print but the "firstname" "lastname", "coursename" aren't appearing. Any insight would be helpful.
homepage.php - I am getting the error on the h3 User info tag
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title> Home Page</title>
<link rel="stylesheet" href="css/allstyle.css" >
</head>
<body>
<div class = "topnavbar">
<ul>
<li><a href = "UserInfo.php" class = "active"> User Info</a></li>
<li><a href = "Homework.php"> Homework </a></li>
<li><a href = "ToDoList.php"> To-Do-List </a></li>
<li><a href = "WishList.php"> Wishlist </a></li>
<li><a href = "MessageBoard.php"> MessageBoard </a></li>
<ul>
</div>
<div id = "main-wrapper">
<center>
<h2>Home Page</h2>
<h3> Welcome <?php echo $_SESSION['username']?> </h3>
<h3> User Info: <?php echo $_SESSION['firstname']?> </h3>
<h3><?php echo $_SESSION['lastname']?></h3>
<img src = "imgs/DefaultAvatar.jpg" class ="avatar"/>
</center>
<form class = "myform" action = "homepage.php" method = "post">
<input name = "logout" type = "submit" id = "logout_btn" value = "Log Out"/><br></a>
</form>
<?php
if(isset($_POST['logout']))
{
session_destroy();
header('location: index.php');
}
?>
</div>
</body>
</html>
register.php
<?php
require 'dbconfig/config.php';
?>
<!DOCTYPE html>
<html>
<head>
<title> Registration Page </title>
<link rel="stylesheet" href="css/allstyle.css" >
</head>
<body>
<div id = "main-wrapper">
<center>
<h2>Registration Form</h2>
<img src = "imgs/DefaultAvatar.jpg" class ="avatar"/>
</center>
<form class = "myform" action = "register.php" method = "post">
<label><b>First Name:</b></label><br>
<input name = "firstname" type = "text" class = "inputvalues" placeholder = "Enter your first name" required/><br>
<label><b>Last Name:</b></label><br>
<input name = "lastname" type = "text" class = "inputvalues" placeholder = "Enter your last name" required/><br>
<label><b>Course Name:</b></label><br>
<input name = "coursename" type = "text" class = "inputvalues" placeholder = "Enter your course name" required/><br>
<label><b>Username:</b></label><br>
<input name = "username" type = "text" class = "inputvalues" placeholder = "Enter your username" required/><br>
<label><b>Password:</b></label><br>
<input name = "password" type = "password" class = "inputvalues" placeholder = "Enter your password" required/><br>
<label><b> Confirm Password:</b></label><br>
<input name = "cpassword" type = "password" class = "inputvalues" placeholder = "Confirm your password" required/><br>
<input name = "submit_btn" type = "submit" id = "signup_btn" value = "Sign Up"/><br>
<a href = "index.php"><input type ="button" id = "back_btn" value = " << Back to Login"/></a>
</form>
</html>
<?php
if(isset($_POST['submit_btn']))
{
//echo '<script type = "text/javascript"> alert("Sign up button clicked") </script>';
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$coursename = $_POST['coursename'];
$username = $_POST['username'];
$password = $_POST['password'];
$cpassword = $_POST['cpassword'];
if($password==$cpassword)
{
$query= " select * from user WHERE username='$username'";
$query_run = mysqli_query($con, $query);
if(mysqli_num_rows($query_run)>0)
{
//Notifying user their username is taken
echo '<script type = "text/javascript"> alert("This username is taken, please try another. ") </script>';
}
else
{
$query = "insert into user values ('$username','$password','$firstname','$lastname','$coursename')";
$query_run = mysqli_query($con,$query);
if($query_run)
{
echo '<script type = "text/javascript"> alert("You have successfully registered! Return to login screen to login. ") </script>';
}
else
{
echo '<script type = "text/javascript"> alert("Error!") </script>';
}
}
}
else
{
echo '<script type = "text/javascript"> alert("Your passwords do not match, please try again.") </script>';
}
}
?>
</div>
</body>
</html>
</div>