SpartanTacoDuckie Posted November 14, 2013 Share Posted November 14, 2013 (edited) Hello, there. I've been messing around with this stuff recently and ran across this error when trying to output a message when not all login fields are filled in, as well as an error when trying to output a successful login message.. These parts of the code are: Check all fields filled: //get the form data $myusername = ($_POST['username']); $mypassword = ($_POST['password']); //check if all fields are filled in if ( (!$myusername) || (!mypassword) ) ) { echo "Please fill in all fields"; exit; } Check if user exists & log in: //check if user exists if ($account ==1) { $_SESSION["username"] = $myusername; $_SESSION["password"] = $mypassword; $_SESSION["userrecord"] = mysql_fetch_assoc($result); echo "You have been logged in successfully. Please click <a href=account.php>here</a> to continue."; } As you can probably tell from the title, the echo does not output onto the webpage. Any help is very much appreciated. Thanks. Edited November 14, 2013 by SpartanTacoDuckie Quote Link to comment https://forums.phpfreaks.com/topic/283883-echo-doesnt-output-help-please/ Share on other sites More sharing options...
KaiSheng Posted November 14, 2013 Share Posted November 14, 2013 For the first part of code on the top, it should be: //get the form data$myusername = ($_POST['username']);$mypassword = ($_POST['password']);//check if all fields are filled inif ( ($myusername == "") || (mypassword == "") ) ) {echo "Please fill in all fields";exit;} --------------- for the second part of code it should be : //check if user exists if (mysqli_num_rows($result) >0) { $row = mysqli_fetch_array($result); $_SESSION['username'] = $row['username']; $_SESSION['password'] = $row['password']; echo "You have been logged in successfully. Please click <a href=account.php>here</a> to continue.";} Quote Link to comment https://forums.phpfreaks.com/topic/283883-echo-doesnt-output-help-please/#findComment-1458179 Share on other sites More sharing options...
SpartanTacoDuckie Posted November 14, 2013 Author Share Posted November 14, 2013 Hmm.... doesn't seem to work correctly either. Still no output. Also, between those two codes I posted I have: //check the form in database $sql = "SELECT = FROM {$usertable} WHERE username = '{$myusername}' AND password = '{$mypassword}'"; $result = mysql_query($sql); $account = mysql_num_rows($result) Quote Link to comment https://forums.phpfreaks.com/topic/283883-echo-doesnt-output-help-please/#findComment-1458181 Share on other sites More sharing options...
KaiSheng Posted November 14, 2013 Share Posted November 14, 2013 Hmm.... doesn't seem to work correctly either. Still no output. Also, between those two codes I posted I have: //check the form in database $sql = "SELECT = FROM {$usertable} WHERE username = '{$myusername}' AND password = '{$mypassword}'"; $result = mysql_query($sql); $account = mysql_num_rows($result) This Query is wrong. Quote Link to comment https://forums.phpfreaks.com/topic/283883-echo-doesnt-output-help-please/#findComment-1458182 Share on other sites More sharing options...
KaiSheng Posted November 14, 2013 Share Posted November 14, 2013 Post your whole code here. Quote Link to comment https://forums.phpfreaks.com/topic/283883-echo-doesnt-output-help-please/#findComment-1458183 Share on other sites More sharing options...
SpartanTacoDuckie Posted November 14, 2013 Author Share Posted November 14, 2013 Here's my whole code: <?php session_start(); ?> <html lang="en"> <html> <head> <title>Login page</title> <meta charset="UTF-8"> <link rel="stylesheet" href="./CSSPAGE.css" </head> <body> <h2 style="text-align:center; color:red">Please log in using the form below.</h2> <br /> <br /> <center> <form name="Login" action="login.php" method="post"> <br /> <br /> Username: <input type="text" placeholder="Enter your username" name="username"> <br /> <br /> Password: <input type="password" placeholder="Enter your password" name="password"> <br /> <br /> <input type="submit" value="Log in"> </center> </form> <?php include 'config.php'; mysql_connect($host, $user, $password) or die(mysql_error()); mysql_select_db($database) or die(mysql_error()); if (isset($_POST['submit']) ) { //get the form data $myusername = ($_POST['username']); $mypassword = ($_POST['password']); //check if all fields are filled in if ( (!$myusername == "") || (!$mypassword == "") ) { echo "Please fill in all fields"; exit; } //check the form in database $sql = "SELECT = FROM {$usertable} WHERE username = '{$myusername}' AND password = '{$mypassword}'"; $result = mysql_query($sql); $account = mysql_num_rows($result); //check if user exists if ($account == 1) { $_SESSION["username"] = $myusername; $_SESSION["password"] = $mypassword; $_SESSION["userrecord"] = mysql_fetch_assoc($result); echo "You have been logged in successfully. Please click <a href=account.php>here</a> to continue."; } } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/283883-echo-doesnt-output-help-please/#findComment-1458186 Share on other sites More sharing options...
KaiSheng Posted November 14, 2013 Share Posted November 14, 2013 (edited) The query should change to $sql = "SELECT id,username,password FROM usertable WHERE username = '$myusername' AND password = '$mypassword'"; Assuming the table in your database is called usertable. please, it shouldn't be with quotes or brackets. standard format in a query, e.g > "SELECT * FROM table WHERE id = '$id'; on the example above, table represents table name without quotes or brackets. id represent the variable in the database $id represent the variable assigned in the php code. --- besides that, if you gonna do some security checks on your form it should be: $myusername = mysql_real_escape_string($_POST['username']); $mypassword = mysql_real_escape_string($_POST['password']); and your register page query should do a SHA1($mypassword) instead of just passing the text value of the password. followed to this login page, The query can changed to `password`='".sha1($mypassword)."' Edited November 14, 2013 by KaiSheng Quote Link to comment https://forums.phpfreaks.com/topic/283883-echo-doesnt-output-help-please/#findComment-1458187 Share on other sites More sharing options...
SpartanTacoDuckie Posted November 14, 2013 Author Share Posted November 14, 2013 (edited) I'm not worried about security on this, really. It's just a test. As for the query, I changed it to what you suggested, with no success. However, my database is phpMyAdmin using XAMPP (LAMP since it's a Linux (at the moment)) so it should work fine, bit it doesn't seem to be. Edited November 14, 2013 by SpartanTacoDuckie Quote Link to comment https://forums.phpfreaks.com/topic/283883-echo-doesnt-output-help-please/#findComment-1458192 Share on other sites More sharing options...
dalecosp Posted November 14, 2013 Share Posted November 14, 2013 In a development environment, if a query fails, print the query, and then print the error message. This often helps you find the issue more quickly. Quote Link to comment https://forums.phpfreaks.com/topic/283883-echo-doesnt-output-help-please/#findComment-1458194 Share on other sites More sharing options...
SpartanTacoDuckie Posted November 14, 2013 Author Share Posted November 14, 2013 Okay, I'll keep that in mind. Thanks. ;3 By the way, I'm calling this from a config.php file, which says: <?php $user = "root"; $host = "localhost"; $password = ""; $database = "login-register"; $usertable = "users"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/283883-echo-doesnt-output-help-please/#findComment-1458196 Share on other sites More sharing options...
KaiSheng Posted November 14, 2013 Share Posted November 14, 2013 (edited) thanks for showing that config.php it's of great help. 1) is your database name really named login-register? if yes, continue to question 2. 2) remove $usertable ="users"; and it should work according to the amendments of the codes in my previous post. ---- Please take note that this config.php supposed to be a file that is used for several pages. it is best to keep only functions and connection to database instead of setting a table there. If you require information from other tables, errors will occur very easily. Edited November 14, 2013 by KaiSheng Quote Link to comment https://forums.phpfreaks.com/topic/283883-echo-doesnt-output-help-please/#findComment-1458197 Share on other sites More sharing options...
SpartanTacoDuckie Posted November 14, 2013 Author Share Posted November 14, 2013 Yes, my login/register testing environment's database is called login-register. As for removing $usertable = "users"; .... The table in my database which holds the id, username, and password is called "users". Removing it didn't end up making it work. Maybe an error with my query? $sql = "SELECT * FROM usertable WHERE username = '$myusername' AND password = '$mypassword'"; $result = mysql_query($sql); $account = mysql_num_rows($result); Quote Link to comment https://forums.phpfreaks.com/topic/283883-echo-doesnt-output-help-please/#findComment-1458200 Share on other sites More sharing options...
KaiSheng Posted November 14, 2013 Share Posted November 14, 2013 your table is called users instead of usertable, am i right? if it's so, please change it to the according name on your query. other than that, the query is fine. calling results from an unknown table results 0 data to appear. Quote Link to comment https://forums.phpfreaks.com/topic/283883-echo-doesnt-output-help-please/#findComment-1458206 Share on other sites More sharing options...
SpartanTacoDuckie Posted November 14, 2013 Author Share Posted November 14, 2013 Hmm, still isn't working right. It's not outputting anything. Quote Link to comment https://forums.phpfreaks.com/topic/283883-echo-doesnt-output-help-please/#findComment-1458207 Share on other sites More sharing options...
KaiSheng Posted November 14, 2013 Share Posted November 14, 2013 Wow.. are you serious. if you don't mind, i'd like to see the login page as well as the config page. the updated ones. Quote Link to comment https://forums.phpfreaks.com/topic/283883-echo-doesnt-output-help-please/#findComment-1458209 Share on other sites More sharing options...
SpartanTacoDuckie Posted November 14, 2013 Author Share Posted November 14, 2013 Config: <?php $user = "root"; $host = "localhost"; $password = ""; $database = "login-register"; ?> Login: <?php session_start(); ?> <html lang="en"> <html> <head> <title>Login page</title> <meta charset="UTF-8"> <link rel="stylesheet" href="./CSSPAGE.css" </head> <body> <h2 style="text-align:center; color:red">Please log in using the form below.</h2> <br /> <br /> <center> <form name="Login" action="login.php" method="post"> <br /> <br /> Username: <input type="text" placeholder="Enter your username" name="username"> <br /> <br /> Password: <input type="password" placeholder="Enter your password" name="password"> <br /> <br /> <input type="submit" value="Log in"> </center> </form> <?php include 'config.php'; mysql_connect($host, $user, $password) or die(mysql_error()); mysql_select_db($database) or die(mysql_error()); if (isset($_POST['submit']) ) { //get the form data $myusername = ($_POST['username']); $mypassword = ($_POST['password']); //check if all fields are filled in if ( (!$myusername == "") || (!$mypassword == "") ) { echo "Please fill in all fields"; exit; } //check the form in database $sql = "SELECT * FROM users WHERE username = '$myusername' AND password = '$mypassword'"; $result = mysql_query($sql); $account = mysql_num_rows($result); //check if user exists if ($account == 1) { $_SESSION["username"] = $myusername; $_SESSION["password"] = $mypassword; $_SESSION["userrecord"] = mysql_fetch_assoc($result); echo "You have been logged in successfully. Please click <a href=account.php>here</a> to continue."; } } ?> </body> </html> >.> Quote Link to comment https://forums.phpfreaks.com/topic/283883-echo-doesnt-output-help-please/#findComment-1458210 Share on other sites More sharing options...
KaiSheng Posted November 14, 2013 Share Posted November 14, 2013 (edited) <?php session_start(); ?> <html lang="en"> <html> <head> <title>Login page</title> <meta charset="UTF-8"> <link rel="stylesheet" href="./CSSPAGE.css" </head> <body> <h2 style="text-align:center; color:red">Please log in using the form below.</h2> <br /> <br /> <center> <form name="Login" action="login.php" method="post"> <br /> <br /> Username: <input type="text" placeholder="Enter your username" name="username"> <br /> <br /> Password: <input type="password" placeholder="Enter your password" name="password"> <br /> <br /> <input type="submit" value="Log in"> </center> </form> <?php include 'config.php'; if (isset($_POST['submit']) ) { //get the form data $myusername = $_POST['username']; $mypassword = $_POST['password']; //check if all fields are filled in if ( ($myusername == "") || ($mypassword == "") ) { echo "Please fill in all fields"; exit; } //check the form in database $link = mysqli_connect($host, $username, $password, $database); $sql = "SELECT * FROM users WHERE username = '$myusername' AND password = '$mypassword'"; $result = mysqli_query($link, $sql) or die(mysqli_error($link)); //check if user exists if (mysqli_num_rows($result) > 0) { $row = mysql_fetch_array($result); $_SESSION["username"] = $row['username']; $_SESSION["password"] = $row['password']; echo "You have been logged in successfully. Please click <a href=account.php>here</a> to continue."; } } ?> </body> </html> Edited November 14, 2013 by KaiSheng Quote Link to comment https://forums.phpfreaks.com/topic/283883-echo-doesnt-output-help-please/#findComment-1458211 Share on other sites More sharing options...
KaiSheng Posted November 14, 2013 Share Posted November 14, 2013 Copy that and use it. It should work fine. Quote Link to comment https://forums.phpfreaks.com/topic/283883-echo-doesnt-output-help-please/#findComment-1458212 Share on other sites More sharing options...
SpartanTacoDuckie Posted November 14, 2013 Author Share Posted November 14, 2013 ...It didn't work... >.> Quote Link to comment https://forums.phpfreaks.com/topic/283883-echo-doesnt-output-help-please/#findComment-1458215 Share on other sites More sharing options...
KaiSheng Posted November 14, 2013 Share Posted November 14, 2013 OMG SERIOUSLY. is there any error? or ?? Quote Link to comment https://forums.phpfreaks.com/topic/283883-echo-doesnt-output-help-please/#findComment-1458217 Share on other sites More sharing options...
SpartanTacoDuckie Posted November 14, 2013 Author Share Posted November 14, 2013 Nope, no error. Just no output of the damn echo.... Quote Link to comment https://forums.phpfreaks.com/topic/283883-echo-doesnt-output-help-please/#findComment-1458218 Share on other sites More sharing options...
KaiSheng Posted November 14, 2013 Share Posted November 14, 2013 What fields do your database have? in the table. Quote Link to comment https://forums.phpfreaks.com/topic/283883-echo-doesnt-output-help-please/#findComment-1458219 Share on other sites More sharing options...
SpartanTacoDuckie Posted November 14, 2013 Author Share Posted November 14, 2013 My table has: id username password Quote Link to comment https://forums.phpfreaks.com/topic/283883-echo-doesnt-output-help-please/#findComment-1458220 Share on other sites More sharing options...
KaiSheng Posted November 14, 2013 Share Posted November 14, 2013 (edited) Let me change your style. Since its only for testing. do 3 pages. 1) login.php (for forms to be filled in) 2) doLogin.php (for background php to be processed) 3) config.php (for establishing database connection) This is config.php <?php $HOST = "localhost"; $USERNAME = "root"; $PASSWORD = ""; $DB = "login-register"; $link = mysqli_connect($HOST, $USERNAME, $PASSWORD, $DB); ?> ------- This is login.php <?php include 'config.php'; // check if user is authenticated session_start(); if (isset($_SESSION['user_id'])) { echo "You are already logged in.<br/>."; } else { } ?> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Login Page</title> </head> <body bgcolor ="lightblue"> <form method="post" action="doLogin.php"> <table> <tr> <td><label for="username">Username:</label></td> <td><input type="text" id="username" name="username" required/></td> </tr> <tr> <td><label for="password">Password:</label></td> <td><input type="password" id="password" name="password" required/></td> </tr> </table> <input type="submit" value="Login" name="submit"/> </form> </body> </html> ---- This is doLogin.php <?php session_start(); include ('config.php'); if (!isset($_SESSION['user_id'])) { if (isset($_POST['username'])) { //retrieve form data $username = $_POST['username']; $password = $_POST['password']; $link = mysqli_connect($HOST, $USERNAME, $PASSWORD, $DB); //match the username and password entered with database record $query = ("SELECT * FROM `users` WHERE `username`='$username' AND `password`='$password'"); $result = mysqli_query($link, $query) or die(mysqli_error($link)); //if record is found, store id and username into session if (mysqli_num_rows($result) >0) { $row = mysqli_fetch_array($result); $_SESSION['user_id'] = $row['id']; $_SESSION['username'] = $row['username']; $msg = '<p><i>Hello, ' . $row['username'] . '!<br />'; $msg .= 'You are logged in.<br /><a href="home.php">Home</a></p>'; } else { //record not found $msg = '<p class="error">Sorry, you must enter a valid username and password to log in.<a href="login.php"> Back</a></p>'; } } } else { $msg = 'You are already logged in.<br /><a href="home.php">Home</a></p>'; } ?> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="style.css" /> <title>Login page</title> <link rel="stylesheet" type="text/css" href="style.css" /> <header><b></b></header> </head> <body bgcolor ="lightblue"> <?php echo $msg; ?> <?php header("refresh:4;url=index.php"); ?> </body> </html> --- Change the query according to your database table name. Edited November 14, 2013 by KaiSheng Quote Link to comment https://forums.phpfreaks.com/topic/283883-echo-doesnt-output-help-please/#findComment-1458221 Share on other sites More sharing options...
SpartanTacoDuckie Posted November 14, 2013 Author Share Posted November 14, 2013 (edited) Now it has errors: Notice: Undefined variable: host in /opt/lampp/htdocs/files/Login-register/login.php on like 37 Notice: Undefined variable: user in /opt/lampp/htdocs/files/Login-register/login.php on like 37 Notice: Undefined variable: password in /opt/lampp/htdocs/files/Login-register/login.php on like 37 Notice: Undefined variable: database in /opt/lampp/htdocs/files/Login-register/login.php on like 38 No database selected Now here's what I don't understand about this: Line 37 is: </head> so it's just closing off the heading... what..? Line 38 is blank; absolutely nothing there. So where is it missing the variables at? Edit: I'm off for today... getting late. I'll check back for any responses tomorrow, try them out, and get back on it. By the way, thanks for the help you've provided so far. Very quick to respond, helpful, and also patient. Thanks. ;3 Edited November 14, 2013 by SpartanTacoDuckie Quote Link to comment https://forums.phpfreaks.com/topic/283883-echo-doesnt-output-help-please/#findComment-1458223 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.