Jump to content

else if problems


dad00

Recommended Posts

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";
}
}
}

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

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";
}

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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}'";

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.