lybat Posted August 13, 2013 Share Posted August 13, 2013 I'm creating a users level login system in php & mysql. However, it leads to a white blank page. I can't figure the error. Any help is much appreciated. Thanks. Form.html <!DOCTYPE html><!--[if lt IE 7]> <html class="lt-ie9 lt-ie8 lt-ie7" lang="en"> <![endif]--><!--[if IE 7]> <html class="lt-ie9 lt-ie8" lang="en"> <![endif]--><!--[if IE 8]> <html class="lt-ie9" lang="en"> <![endif]--><!--[if gt IE 8]><!--> <html lang="en"> <!--<![endif]--><head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <title>MIS Electronic Request</title> <link rel="stylesheet" href="css/style.css"> </head><body> <br> <form method="post" action="main.php" class="login"> <p> <label for="login">Username:</label> <input type="text" name="myusername" id="myusername" value="Employee id"> </p> <p> <label for="password">Password:</label> <input type="password" name="mypassword" id="mypassword" value="4815162342"> </p> <p class="login-submit"> <button type="submit" class="login-button">Login</button> </p> </form></body></html> Main.php <?phpsession_start();$host="localhost";$username="root";$password="******";$db_name="htdocs";$tbl_name="users";mysql_connect("$host", "$username", "$password")or die("Unable to connect to MySQL: " . mysql_error());mysql_select_db("$db_name")or die("Unable to select database: " . mysql_error());if (isset($_POST['submit'])){// username and password sent from form$myusername=$_POST['myusername'];$mypassword=$_POST['mypassword'];$myusername = stripslashes($myusername);$mypassword = stripslashes($mypassword);$myusername = mysql_real_escape_string($myusername);$mypassword = mysql_real_escape_string($mypassword);$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";$result=mysql_query($sql);// Mysql_num_row is counting table row$count=mysql_num_rows($result);if($count==1){ if (user_level == 1) { $_SESSION['myusername'] = $myusername; $_SESSION['mypassword'] = $mypassword; header("Location:Admin_home.php"); } else if (user_level == 2) { $_SESSION['myusername'] = $myusername; $_SESSION['mypassword'] = $mypassword; header("Location:home.php"); }}else {echo "Wrong Username or Password";}}?> Quote Link to comment Share on other sites More sharing options...
davidannis Posted August 13, 2013 Share Posted August 13, 2013 try adding name="submit" to the submit button or check like this: if (isset($_POST['myusername']) || isset($_POST['mypassword'])) Quote Link to comment Share on other sites More sharing options...
lybat Posted August 13, 2013 Author Share Posted August 13, 2013 Thanks for the reply. The blank page issue has been resolved. But this time i got this error: Error: Notice: Use of undefined constant user_level - assumed 'user_level' in C:\wamp\www\htdocs\main.php on line 34Notice: Use of undefined constant user_level - assumed 'user_level' in C:\wamp\www\htdocs\main.php on line 39 Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted August 13, 2013 Share Posted August 13, 2013 You have missed the $ off both the following lines. if (user_level == 1) { if (user_level == 2) { Quote Link to comment Share on other sites More sharing options...
lybat Posted August 14, 2013 Author Share Posted August 14, 2013 Hi i already did that. still error.. if($count==1){ if ($user_level == 1) { $_SESSION['myusername'] = $myusername; $_SESSION['mypassword'] = $mypassword; header("Location:Admin_home.php"); } else if ($user_level == 2) { $_SESSION['myusername'] = $myusername; $_SESSION['mypassword'] = $mypassword; header("Location:home.php"); }} Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted August 14, 2013 Share Posted August 14, 2013 You have updated the wrong file then, my suggestion would fix the 2 "Use of undefined constant" errors. Quote Link to comment Share on other sites More sharing options...
davidannis Posted August 14, 2013 Share Posted August 14, 2013 and you still should have a space after Location: header("Location:Admin_home.php"); header("Location:home.php"); but you don't need the space in else if you can use elseif Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted August 14, 2013 Share Posted August 14, 2013 and you still should have a space after Location: header("Location:Admin_home.php"); header("Location:home.php"); but you don't need the space in else if you can use elseif You don't need the space after "Location:", if you had tried it, you would see it works without the space. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted August 14, 2013 Share Posted August 14, 2013 the error message probably did change to an undefined variable message. the php variable $user_level is not defined anywhere in the posted code. where do you expect it to get its value from and where is your code setting it to that value? Quote Link to comment Share on other sites More sharing options...
davidannis Posted August 14, 2013 Share Posted August 14, 2013 Good catch Mac-gyver. The record is never fetched. I bet the OP is looking for something like $row['user_level'] from the database row that never got fetched. Quote Link to comment Share on other sites More sharing options...
lybat Posted August 15, 2013 Author Share Posted August 15, 2013 Guys, thanks for all the comment. How can i defined or fetched the value of user_level from the database? user_level = $row['user_level'] is this correct? Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted August 15, 2013 Share Posted August 15, 2013 no, it's not correct. i recommend that you read a php/mysql book or tutorial as you are currently not aware of even the syntax for a php variable and you will also need to know how to fetch a row from the result set that a query returns. Quote Link to comment Share on other sites More sharing options...
davidannis Posted August 15, 2013 Share Posted August 15, 2013 You don't need the space after "Location:", if you had tried it, you would see it works without the space. You are right. The space is is ignored and multiple spaces after the colon should work too (according to the RFC). I thought I had an issue a few moths ago that I solved by adding a space after the colon in a header, but I must be remembering it incorrectly. I stand corrected. Sorry. Quote Link to comment Share on other sites More sharing options...
lybat Posted August 22, 2013 Author Share Posted August 22, 2013 still can't figure it out until now.. any help? Thanks! Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted August 22, 2013 Share Posted August 22, 2013 do you still have a problem or error with your code after all this time? you should probably post your current code as well. Quote Link to comment Share on other sites More sharing options...
Twysted Posted August 22, 2013 Share Posted August 22, 2013 still can't figure it out until now.. any help? after this $count=mysql_num_rows($result); add this $row=mysql_fetch_array($result); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.