dad00 Posted March 1, 2009 Share Posted March 1, 2009 why wont this code work 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"; } } Link to comment https://forums.phpfreaks.com/topic/147444-else-if-problems/ Share on other sites More sharing options...
Mad Mick Posted March 1, 2009 Share Posted March 1, 2009 What doesn't work? Why not check for the existence of $username or $email with a SQL query: $result=mysql_query(SELECT * FROM users WHERE username='$username'); $num_records=mysql_num_rows($result); If $numrecords is greater than 0 then username in use. Not sure how you are doing it. Link to comment https://forums.phpfreaks.com/topic/147444-else-if-problems/#findComment-773933 Share on other sites More sharing options...
jackpf Posted March 1, 2009 Share Posted March 1, 2009 Well, merely from the code you've posted, you have an extra } on the end. But unless you actually specify what isn't working, no one can help you. Link to comment https://forums.phpfreaks.com/topic/147444-else-if-problems/#findComment-773955 Share on other sites More sharing options...
dad00 Posted March 1, 2009 Author Share Posted March 1, 2009 the actual code itself dosent work i can use the same username as 1 that already exists all my code: 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"; } } Link to comment https://forums.phpfreaks.com/topic/147444-else-if-problems/#findComment-774000 Share on other sites More sharing options...
Mad Mick Posted March 1, 2009 Share Posted March 1, 2009 $rows['username'] is updated everytime the while loop runs bringing in the next row from the database. So you are effectively just checking against the last line of the database not all of them. You can either put the error checking within the while loop or (better in my opinion) lose the while loop and do the error checking the way I suggested. Oh and "Succesfull" is spelt "Successful" - sorry to be petty... Link to comment https://forums.phpfreaks.com/topic/147444-else-if-problems/#findComment-774010 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.