dad00 Posted February 21, 2009 Share Posted February 21, 2009 ive been trying to make my register form better and ive used alot of if's and else's so some1 told me to use elseif's so i did and i think ive seriously messed it up if(strlen($username) == 0 || strlen($password) == 0){ echo "Error please try again"; }elseif($rows['username'] == $username){ echo "Error username already in use"; }elseif($rows['email'] == $email){ if($rows['email'] == $email){ echo "Error email address already in use"; }elseif(mysql_query("INSERT INTO users (username,password,email) VALUES ('$username','$password','$email')")){ echo "Register Succesfull"; }else{ echo "Register failed"; } } } Quote Link to comment Share on other sites More sharing options...
jackpf Posted February 21, 2009 Share Posted February 21, 2009 I usually put a space between else and if. I don't know if it's mandatory, but it's what I do, and my stuff works fine. You could see if it works... What sort of error message do you get? Also, the whole strlen business, you could just use empty()? Looks a lot nicer Quote Link to comment Share on other sites More sharing options...
Cal Posted February 21, 2009 Share Posted February 21, 2009 It's very badly structured, I definitely wouldn't use it. Quote Link to comment Share on other sites More sharing options...
dad00 Posted February 21, 2009 Author Share Posted February 21, 2009 how should it be structured and i dont get a error it just dosent work Quote Link to comment Share on other sites More sharing options...
Dowdy Posted February 21, 2009 Share Posted February 21, 2009 try this if(strlen($username) == 0 || strlen($password) == 0) { echo "Error please try again"; } else if($rows['username'] == $username) { echo "Error username already in use"; } else if($rows['email'] == $email) { echo "Error email address already in use"; } else { mysql_query("INSERT INTO users (`username`, `password`, `email`) VALUES ('$username','$password','$email')") echo "Register Succesfull"; } hope this helps it should be easier to debug. like this you had a lot of excess close braces. if you tab every time you open a brace you'll find it easier to see whats going on Quote Link to comment Share on other sites More sharing options...
dad00 Posted February 21, 2009 Author Share Posted February 21, 2009 now i got the error Parse error: parse error in C:\wamp\www\register.php on line 63 Quote Link to comment Share on other sites More sharing options...
josephman1988 Posted February 21, 2009 Share Posted February 21, 2009 What is line 63? Quote Link to comment Share on other sites More sharing options...
jackpf Posted February 22, 2009 Share Posted February 22, 2009 hope this helps it should be easier to debug. like this you had a lot of excess close braces. if you tab every time you open a brace you'll find it easier to see whats going on Tbh, everyone EVERYWHERE should code like this. When you've got a script a thousand lines long, I'd rather be looking at this than what you had before Lesson of the day- It's worth keeping your scripts neat. Quote Link to comment Share on other sites More sharing options...
Cal Posted February 22, 2009 Share Posted February 22, 2009 <?php if($_POST['submit']){ if($_POST['username'] && $_POST['password']){ if(mysql_num_rows($data) == 1){ if($data2['active'] == 1){ if($data2['banned'] == 0){ }else{ $message = "You have been banned by an administrator."; } }else{ $message = "Your account is not activated, if you have just registered check your emails for an activation email."; } }else{ $message = "Incorrect username or password."; } }else{ $message = "Enter a username and password."; } } ?> I think it's best to code in a structure like that. Quote Link to comment Share on other sites More sharing options...
jackpf Posted February 22, 2009 Share Posted February 22, 2009 Hmm...I prefere like if(foo) { bar(); } I find it a lot easier to see what statements are in what conditions. Personal preference I guess. Quote Link to comment Share on other sites More sharing options...
dad00 Posted February 22, 2009 Author Share Posted February 22, 2009 i still cant get it to work without errors Quote Link to comment Share on other sites More sharing options...
redarrow Posted February 22, 2009 Share Posted February 22, 2009 can you post your current code please so i can look m8. Quote Link to comment Share on other sites More sharing options...
Cal Posted February 22, 2009 Share Posted February 22, 2009 whats the error Quote Link to comment Share on other sites More sharing options...
dad00 Posted February 22, 2009 Author Share Posted February 22, 2009 current code is if(strlen($username) == 0 || strlen($password) == 0) { echo "Error please try again"; } else if($rows['username'] == $username) { echo "Error username already in use"; } else if($rows['email'] == $email) { echo "Error email address already in use"; } else { mysql_query("INSERT INTO users (username,password,email) VALUES ('$username','$password','$email')") echo "Register Succesfull"; } error is: Parse error: parse error in C:\wamp\www\register.php on line 63 and line 62,63,64 is mysql_query("INSERT INTO users (username,password,email) VALUES ('$username','$password','$email')") echo "Register Succesfull"; } Quote Link to comment Share on other sites More sharing options...
Cal Posted February 22, 2009 Share Posted February 22, 2009 mysql_query("INSERT INTO users (username,password,email) VALUES ('$username','$password','$email')") You're missing the semi-colon on the end of that line. Quote Link to comment Share on other sites More sharing options...
dad00 Posted February 22, 2009 Author Share Posted February 22, 2009 now ive got the error Parse error: parse error in C:\wamp\www\register.php on line 72 and line 72 is the very end of the document and there isnt any php code there Quote Link to comment Share on other sites More sharing options...
Cal Posted February 22, 2009 Share Posted February 22, 2009 Can you post the whole script.? Quote Link to comment Share on other sites More sharing options...
redarrow Posted February 22, 2009 Share Posted February 22, 2009 just add another } <<<< to the end Quote Link to comment Share on other sites More sharing options...
dad00 Posted February 22, 2009 Author Share Posted February 22, 2009 theres no error but the code dosent do what its supposed to its supposed to stop you using the same username or email then whats in the database heres all my code <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Login</title> </head> <body> <?php $hostname = "localhost";//The database host $username = "root";//The database username $password = "";//The database password $db = "account"; $conn = mysql_connect($hostname,$username,$password); if(!$conn) { die( "Problem creating database connection, please contact the system administrator. \n\r<br> Here is the exact error message: ".mysql_error() ); } $db_conn = mysql_select_db($db); if(!$db_conn) { die( "Problem finding database, please contact the system administrator. \n\r<br> Here is the exact error message: ".mysql_error() ); } if(isset($_POST['login'])) { $sql = "SELECT * FROM users"; $sql = mysql_query($sql) or die(mysql_error()); //print_r($sql); while($rows = mysql_fetch_assoc($sql)) { $usernamec = $rows['username']; $emailc = $rows['email']; } $username = trim($_POST['username']); //gets the username from the text box 'username' and removes any blank space $password =trim($_POST['password']); //same as above just for password $email =trim($_POST['email']); if(strlen($username) == 0 || strlen($password) == 0) { echo "Error please try again"; } else if($rows['username'] == $username) { echo "Error username already in use"; } else if($rows['email'] == $email) { echo "Error email address already in use"; } else { mysql_query("INSERT INTO users (username,password,email) VALUES ('$username','$password','$email')"); echo "Register Succesfull"; } } ?> <form action="" method="post"> Username<input type="text" name="username" value=""><br> Password<input type="password" name="password" value=""><br> Email<input type="text" name="email" value=""><br> Verify Email <input type="text" name="Verify Email Address" value=""><br> <input type="submit" name="login" value="Register"> </form> Quote Link to comment Share on other sites More sharing options...
dad00 Posted February 22, 2009 Author Share Posted February 22, 2009 anyone? Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted February 22, 2009 Share Posted February 22, 2009 Hmm...I prefere like if(foo) { bar(); } I find it a lot easier to see what statements are in what conditions. Personal preference I guess. you'd be surprised there are a lot of different indent styles http://en.wikipedia.org/wiki/Indent_style as long as you make it readable and use one style throughout one project is what should matter @dad00 What are you trying to do create a register form or a login form if you want to check if a user already exist simply make the query like that $username=mysql_real_escape_string($_POST['username']); $sql = "SELECT * FROM users WHERE username='{$username}'"; 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.