I have the following code in my php files, but i am having a following error while loading my page?

"Notice: Undefined variable: username in C:\xampp\htdocs\fyp\cms\cms.php on line 48"

My page is successfully loading up, but username is not being shown in the page and the error is popped up in it.
Can someone tell whats the error here?

login.php

<?php include_once("../includes/connection.php");?>
<!DOCTYPE html>
<?php
session_start();
 ?>
<html lang='en'>
<head>
    <meta charset="UTF-8" /> 
    <title>
        HTML Document Structure
    </title>
    <link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>

<div id="wrapper">

<?php
$current_page = $_SERVER['PHP_SELF'];
?>
    <form name="login-form" class="login-form" action= "<?php echo $current_page;?>" method="post">

        <div class="header">
        <h1>Login Form</h1>
        <span>Fill out the form below to login to my super awesome imaginary control panel.</span>
        </div>

        <div class="content">
        <input name="username" type="text" class="input username" placeholder="Username" />
        <div class="user-icon"></div>
        <input name="password" type="password" class="input password" placeholder="Password" />
        <div class="pass-icon"></div>     
        </div>



        <div class="footer">
        <input type="submit" name="button" value="submit" class="button" />
        <a href="../qmc-reg/reg.php" style="color:#000" >  Register</a>
        </div>

    </form>
    <?php
if (isset($_POST['username']) && isset($_POST['password'])){
$username = $_POST['username'];
$password = $_POST['password'];

$query_one  = "SELECT * ";
$query_one .= "FROM users ";
$query_one .= "WHERE user_name = '".$username."' ";
$query_one .= "AND user_pass = '".$password."' ";
$query_one .= "LIMIT 1";

$result = mysql_query($query_one) or die(mysql_error());
$count = mysql_num_rows($result);

if ($count == 1){
$something  = mysql_fetch_array($count);
$_SESSION['user_id'] = $something['user_id'];
$_SESSION['user_name'] = $something['user_name'];
header("location: ../cms/cms.php");
}
else{
    echo "<div> Invalid Credentials </div>";
}
}
?>

</div>
<div class="gradient"></div>

</body>
</html>

CMS.php

<?php 
session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css" />
<title>QMC Home</title>
</head>

<body>
<?php
$connection = mysql_connect('localhost','root','');
if(!$connection){
die("Database Connection Failed". mysql_error());
}
$select_db = mysql_select_db('hamdard_attendance');
if(!$select_db){
die("Database Connection Failed" . mysql_error());
}
if (isset($_POST['user_name'])){
$username = $_POST['user_name'];    
}
?>
<div id="container">
        <div id="header">
            <h1 style="text-align:left">Quality Management<span class="off"> Cell</span></h1>

        </div>   

        <div id="menu">
            <ul>
                <li class="menuitem"><a href="cms.php">Home</a></li>
                <li class="menuitem"><a href="cms-attendance.php">Attendance</a></li>
                <li class="menuitem"><a href="cms-courses.php">Courses</a></li>
                <li class="menuitem"><a href="cms-settings.php">Settings</a></li>

            </ul>
            <a style="text-align:right" href="cms-logout.php">Logout</a>
        </div>

        </div>        
        <div id="content">
        <div id="content_top"></div>
        <div id="content_main">
<p> Welcome <?php echo $_SESSION['user_name'];?> </p>
<?php                                
$query_three = "SELECT s.stdnt_name, c.course_name FROM students s inner JOIN student_courses sc ON sc.student_id = s.stdnt_rfid_tag INNER JOIN users u ON s.stdnt_name = u.name INNER JOIN courses c ON c.course_id = sc.course_id where u.user_id = '".$username."'"; 
$result_attendance3 = mysql_query($query_three) or die(mysql_error());
echo "<table border='1': border-color: silver;'>";
echo "<tr>";
echo "<td align='center' width='200'>" . "<h4>"."Student Name" ."</h4>". "</td>";
echo "<td align='center' width='200'>". "<h4>"."Course Name" ."</h4>". "</td>";
echo "</tr>";
echo "</table>";
while($row = mysql_fetch_array($result_attendance3)){
echo "<br />";
//echo "<td align='center' width='200'>".$row['st_classes_attempt'] . "</td>";
echo "<table border='1': border-color: silver;'>";
echo "<tr>";
echo "<td align='center' width='200'>".$row['stdnt_name'] . "</td>";
echo "<td align='center' width='200'>".$row['course_name'] . "</td>";
//echo "<td align='center' width='200'>".$row['st_classes_attempt'] . "</td>";
echo "</tr>";
echo "</table>";
} 
?>

            <p>&nbsp;</p>
            <p>&nbsp;</p>

        <div id="content_bottom"></div>


      </div>
   </div>
</body>
</html>

You are POSTing data to login.php (self) page on line 21 in action= "<?php echo $current_page;?>", instead POST to CMS.php using action='CMS.php', it should solve your problem.

commented: This is a valid solution. Even though it doesn't cover the session. +0

but after than the query written in the login.php won't work my friend. Thats my point. Isn't any way to solve it?

If you do want to continue using login.php to run your sql query, as you are populating a session variable, then you would need to use the session variable in cms.php to get the username back out.

replace your 'post' check in cms.php with a $_SESSION check.

if (isset($_SESSION['user_name']))
{
    $username = $_SESSION['user_name'];
}

A word of caution:
Sessions can be insecure. Please read this article

Its still showing me the same error. When i replaced post with Session.

Notice: Undefined variable: username in C:\xampp\htdocs\fyp\cms\cms.php on line 47

I have gone through your code again, and found that you have not got session variables obtained by While loop under your query in login.php as they are missing to work on next (CMS.php) file, when you make session variables they must be first available inside your while loop(which you missed).Do as follow:

$something  = mysql_fetch_array($count);
while ($something = mysql_fetch_array($sql)){
    $something['user_id'];
    $something['user_name'];
    $something['user_pass'];

if ($count > 0){
session_start();
$_SESSION['user_id'] = $something['user_id'];
$_SESSION['user_name'] = $something['user_name'];
$_SESSION['user_name'] = $something['user_name'];
session_write_close();
header("location: cms.php");
}
else {
   echo "<div> Invalid Credentials </div>";
}
}

and on next CMS.php file you should get this username variable with $_SESSION('user_name']; not with POST.

also you must have session_start() before you make them, and also put session_write_close(); after they are finished.I have run your script on my localhost and it ran perfectly,with no problem! no invalid mesaages etc.Hope it will be fine now ! :)

commented: Nice catch +1

Brother thanks for your help, but i am having, "Parse error: syntax error, unexpected end of file in C:\xampp\htdocs\fyp\qmc-login\login.php on line 79" When i update my code according to your prescribed way. Can u help?

<?php
session_start();
 ?>
<?php include_once("../includes/connection.php"); ?>
<!DOCTYPE html>
<html lang='en'>
<head>
    <meta charset="UTF-8" /> 
    <title>
        HTML Document Structure
    </title>
    <link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>

<div id="wrapper">

<?php
$current_page = $_SERVER['PHP_SELF'];
?>
    <form name="login-form" class="login-form" action= "<?php echo $current_page; ?>" method="post">

        <div class="header">
        <h1>Login Form</h1>
        <span>Fill out the form below to login to my super awesome imaginary control panel.</span>
        </div>

        <div class="content">
        <input name="username" type="text" class="input username" placeholder="Username" />
        <div class="user-icon"></div>
        <input name="password" type="password" class="input password" placeholder="Password" />
        <div class="pass-icon"></div>     
        </div>



        <div class="footer">
        <input type="submit" name="button" value="submit" class="button" />
        <a href="../qmc-reg/reg.php" style="color:#000" >  Register</a>
        </div>

    </form>
<?php
if (isset($_POST['username']) && isset($_POST['password'])){
$username = $_POST['username'];
$password = $_POST['password'];

$query_one  = "SELECT * ";
$query_one .= "FROM users ";
$query_one .= "WHERE user_name = '".$username."' ";
$query_one .= "AND user_pass = '".$password."' ";
$query_one .= "LIMIT 1";

$result = mysql_query($query_one) or die(mysql_error());
$count = mysql_num_rows($result);

if ($count == 1){
$something  = mysql_fetch_array($count);
while ($something = mysql_fetch_array($sql)){
        $something['user_id'];
        $something['user_name'];
        $something['user_pass'];
if ($count > 0){

$_SESSION['user_id'] = $something['user_id'];
$_SESSION['user_name'] = $something['user_name'];
$_SESSION['user_name'] = $something['user_name'];
session_write_close();
header("location: ../cms/cms.php");
}
else{
    echo "<div> Invalid Credentials </div>";
}
}
?>
</div>
<div class="gradient"></div>
</body>
</html>

Thats no problem! remove if {} on line 57 and also its closing bracket at end, you also need to change $sql query variable on line 59 with your own $result var, as on while loop query variable is incorrect.Hope you understand now ! :) for error debugging use DW instead of Note Pad, as Dreamweaver will show you errors straight away which you can handle easily !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.