serien Posted May 29, 2012 Share Posted May 29, 2012 I'm not quite sure what is wrong with this code. It is all the more frustrating because it works fine when implemented on my localhost, but once I put it live on the web, it fails. It is a simple login check, but when I try to submit my information, every single time the error "You must enter a username" comes up. However, I have entered text into the username box. I have no idea what could be causing this! The functions <h3>Login</h3> <?php if(isset($_POST['login'])) // Has information been entered? { $username = mysql_real_escape_string($_POST['username']); $password = mysql_real_escape_string($_POST['password']); $error = login($username, $password); $error = isset($error) ? $error: ""; echo $error; // Report login status } if(isset($_SESSION['userid'])) { // Is user already logged in? header('Location:/'); } else { // If not, display login form ?> <!-- Login Form --> <form class="prettyform" action="" method="post"> <p>Username: <input type="text" name="username"></input></p> <p>Password: <input type="password" name="password"></input></p> <input type="submit" value="Login" name="login"> </form> <?php } function login($username, $password) // CHECKS USER LOGIN { if ($username != NULL) // Username entered? { if ($password != NULL) //Password entered? { if (existing_username($username) == TRUE) // Does username exist? { if (valid_match($username, $password) == TRUE) // Is entered information valid? { $error = "You have successfully logged in!"; $query = mysql_query("SELECT * FROM users WHERE username = '$username'"); $userid = mysql_fetch_array($query); $_SESSION['userid']=$userid['userid']; // Set session variable } else { $error = "<br>That username/password combination is invalid."; } } else { $error = "<br>That username does not exist."; } } else { $error = "<br>You must enter a password"; } } else { $error = "<br>You must enter a username"; } return $error; } ?> Any help would be appreciated! Let me know if you need to see anything more =] It is this page: http://dellusionary.com/login.php Quote Link to comment https://forums.phpfreaks.com/topic/263346-form-data-doesnt-seem-to-be-posting/ Share on other sites More sharing options...
.josh Posted May 29, 2012 Share Posted May 29, 2012 where are you defining existing_username() and valid_match() ? Also, I *assume* that valid_match and existing_username() both perform db queries...and then you have yet another query to get and assign user id to a session var...why not do a single query? Quote Link to comment https://forums.phpfreaks.com/topic/263346-form-data-doesnt-seem-to-be-posting/#findComment-1349625 Share on other sites More sharing options...
serien Posted May 29, 2012 Author Share Posted May 29, 2012 Here are those two functions. The if wasn't even getting that far so I figured they couldn't be the problem. As far as streamlining into one query, I am hacking the basics out before I go back and really get everything composited into neater style ^^. I have a lot of work ahead of me! That is a good idea though. (code tags failed for some reason) <?php function existing_username($username) // CHECKS IF USERNAME EXISTS { $query = mysql_query("SELECT * FROM users WHERE username = '$username'" ); $result = mysql_num_rows($query); return !($result == 0); } function valid_match($username, $password) // CHECKS IF LOGIN VALID { $encryptpass = md5($password.$username); $query= mysql_query("SELECT * FROM users WHERE username = '$username'AND password ='$encryptpass'"); $result = mysql_num_rows($query); return ($result == 1); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/263346-form-data-doesnt-seem-to-be-posting/#findComment-1349627 Share on other sites More sharing options...
ManiacDan Posted May 29, 2012 Share Posted May 29, 2012 Have you done any debugging on your own? If the form data doesn't seem to be posting, print out the post data and see for yourself. Your username isn't being set, so echo it out and check it. You cannot possibly fix a script simply by staring at it. Quote Link to comment https://forums.phpfreaks.com/topic/263346-form-data-doesnt-seem-to-be-posting/#findComment-1349631 Share on other sites More sharing options...
serien Posted May 29, 2012 Author Share Posted May 29, 2012 It's definitely the form data, it doesn't echo out. the submit data obviously passes, however, because the function runs. No clue. EDIT: It may have something to do with the fact that it works on localhost and not on the host server. Does anyone have any idea what could cause that? Quote Link to comment https://forums.phpfreaks.com/topic/263346-form-data-doesnt-seem-to-be-posting/#findComment-1349632 Share on other sites More sharing options...
wigwambam Posted May 29, 2012 Share Posted May 29, 2012 Have you opened a connection to the database? It's required to use the mysql_real_escape_string() function. Quote Link to comment https://forums.phpfreaks.com/topic/263346-form-data-doesnt-seem-to-be-posting/#findComment-1349633 Share on other sites More sharing options...
.josh Posted May 29, 2012 Share Posted May 29, 2012 so at the very top of your script, outside of all conditions, if you put the following: echo "<pre>"; print_r($_POST); echo "</pre>"; You see the data you submitted? If yes...okay, where in your script do you actually connect to your database? I see a whole lot of database queries happening... do you actually connect to/select your database somewhere? Did you test your queries to see if they are returning what they are supposed to be returning, or if they are throwing an error? (turn error reporting on by adding error_reporting(-1); to top of script or echo mysql_error after each query). [/code] Quote Link to comment https://forums.phpfreaks.com/topic/263346-form-data-doesnt-seem-to-be-posting/#findComment-1349635 Share on other sites More sharing options...
serien Posted May 29, 2012 Author Share Posted May 29, 2012 I thought that might be the problem, but I see nothing wrong with my connection query. I've checked over the spelling 5-6 times, and the syntax is the same when I connect to localhost. That is the most obvious answer, though, so I will check again Quote Link to comment https://forums.phpfreaks.com/topic/263346-form-data-doesnt-seem-to-be-posting/#findComment-1349638 Share on other sites More sharing options...
.josh Posted May 29, 2012 Share Posted May 29, 2012 I thought that might be the problem, but I see nothing wrong with my connection query. I've checked over the spelling 5-6 times, and the syntax is the same when I connect to localhost. That is the most obvious answer, though, so I will check again looking at the syntax helps, but is not good enough. So you spelled it right...so what? Do you actually have a database on your server, did you check that? Do you have error reporting turned on? Are you checking the results of your query? You can't just eyeball! Quote Link to comment https://forums.phpfreaks.com/topic/263346-form-data-doesnt-seem-to-be-posting/#findComment-1349641 Share on other sites More sharing options...
serien Posted May 29, 2012 Author Share Posted May 29, 2012 Ok, I will talk with the server mod, for some reason the user they gave me doesn't have access. I can login to phpmyadmin with it, but it is denying the query in the code. Thanks guys ^^ I'm still learning how error reporting works Quote Link to comment https://forums.phpfreaks.com/topic/263346-form-data-doesnt-seem-to-be-posting/#findComment-1349644 Share on other sites More sharing options...
serien Posted May 29, 2012 Author Share Posted May 29, 2012 SOLVED! It was a url mistake. >< gotta love coding! The smallest things can completely mess you up. Thanks for all the help though guys! Quote Link to comment https://forums.phpfreaks.com/topic/263346-form-data-doesnt-seem-to-be-posting/#findComment-1349664 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.