hilda Posted December 27, 2012 Share Posted December 27, 2012 HiHere is the login page <?session_start(); session_register("login_session"); $username = "";$password = ""; if (isset($_POST["Username"])) $username = $_POST["Username"];if (isset($_POST["Password"])) $password = $_POST["Password"]; if ($username != "" && $password != "") { $link = mysql_connect("localhost","root","root"); mysql_select_db("database"); $sql = "SELECT * FROM trainee WHERE password='"; $sql.= $password."' AND username='".$username."'"; $result = mysql_query($sql); if (mysql_fetch_row($result) != false) { $_SESSION["login_session"] = true; if ($username = "trainee"){ header("Location: student.php");} }else echo '<script type="text/javascript"> window.onload=function(){alert("It is incorrect! Please enter again!");} </script>'; mysql_close($link);}?> Below is the form for login <div class="sidebar"> <div class="sidebar_item" id="login_form"> <p> </p> <form method="post" action="login_stu.php"> <fieldset> <legend align="center">Trainee Login</legend> <p> <label for="ID"> Username:</label> <input type="text" name="Username" /> </p> <p> <label for="PASSWORD"> Password:<br /> </label> <input type="password" name="Password" /> </p> <p align="center"> <input type="submit" name="submit" value="Login" style="height:1.7em; width:3.5em;"/> </p> </fieldset> </form> </div> </div> I got a page requiring me to display the usernameAnd I have tried the following code but failed. <?session_start();echo $_SESSION['username'];?> <?session_start();echo $username;?> Any idea? Thank you. Link to comment Share on other sites More sharing options...
Don E Posted December 27, 2012 Share Posted December 27, 2012 Looks like you're forgetting an extra equal sign here: if ($username = "trainee") To check for equality: if ($username == "trainee") Link to comment Share on other sites More sharing options...
kanchatchai Posted December 28, 2012 Share Posted December 28, 2012 you forgot to register$_SESSION['username'] Link to comment Share on other sites More sharing options...
jardrake Posted December 28, 2012 Share Posted December 28, 2012 Since you never set $_SESSION['username'] equal to anything, it will only echo out null. Also, $username = "trainee" is silently failing because you are setting equal to and not testing if equal to. This is a remarkably common mistake of using = when you meant to use ==. Link to comment Share on other sites More sharing options...
westman Posted December 31, 2012 Share Posted December 31, 2012 (edited) use <?php echo $username; ?> in your html body Edited December 31, 2012 by westman Link to comment Share on other sites More sharing options...
Mudsaf Posted January 7, 2013 Share Posted January 7, 2013 (edited) You could to check empty fields with php empty() function if (isset($_POST["Username"])) $username = $_POST["Username"];if (isset($_POST["Password"])) $password = $_POST["Password"]; if ($username != "" && $password != "") { to if (isset($_POST['Username']) || isset($_POST['Password']) || !empty($_POST['Username']) || !empty($_POST['Password']) {//The POST username & passworx exist + not empty } else {//There was no username or password given} Edited January 7, 2013 by Mudsaf Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now