Jump to content

problem with if and else ?


gammaman

Recommended Posts

I have the following which is pulled from a form.

 

If the passwords do not match it displays "passwords do not match"

 

If the user name and two passwords match and the user does not exist it adds a new user.

and say "New user added"

 

However if the user already exists and the passwords match instead of saying "user already exists"

It says "New User Added" but does not add it.

 

I think it is with the if else statments

 

   <body>
<?php

$Conn=mysql_connect("localhost","fierm","13183");

if(!$Conn){ 
   echo "failed";
  } 
  else{ 
mysql_select_db("fierm");


  $User=$_POST["user"];
  $Pass=$_POST["pass"];
  $Conf=$_POST["confirm"];

  if($Pass==$Conf){ 
  
  $result=mysql_query("select usr_name, FROM user WHERE usr_name='$User'");
  $cou=mysql_num_rows($result);
  
  
  
     if($cou>0) 
     { 
       echo "user already exists";  
  }else{ 
     mysql_query("Insert into user (usr_name,pwd) Values ('$User','$Pass')");
    
     echo "New User Was Added";
  
  }

}   
else{
    echo "passwords do not match";
  } 
}     
?>
</body>

 

Link to comment
Share on other sites

Ok, well show us the full code, I can tell you're using POST, but are the input names the same as the ones you're passing? Your code is correct. You could also use error checking

 

mysql_query("whatever") or die(mysql_error());

Link to comment
Share on other sites

   <body>
<?php

$Conn=mysql_connect("localhost","fierm","13183");

if(!$Conn){ 
   echo "failed";
  } 
  else{ 
mysql_select_db("fierm");


  $User=$_POST["user"];
  $Pass=$_POST["pass"];
  $Conf=$_POST["confirm"];

  	if($Pass == $Conf){ 
  		$result=mysql_query("SELECT * FROM user WHERE usr_name='$User' AND `password`='{$Pass}' LIMIT 1;");
  		$cou=mysql_num_rows($result);
  		if($cou>0) 
    	{ 
    		echo "user already exists";  
  		}
  		else{ 
  			$result = mysql_query("SELECT * FROM user WHERE usr_name='{$User}' LIMIT 1;");
  			if (mysql_num_rows($result) != 1){
     			mysql_query("Insert into user (usr_name,pwd) Values ('$User','$Pass')");
    			echo "New User Was Added";
  			}
  		}
	}   
else{
    	echo "passwords do not match";
  	}	 
}     
?>
</body>

Link to comment
Share on other sites

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<title>Change A Department</title>
</head>
<?php

?>
<body>

<form action="checknew.php" method="post">

UserName:<input name="user" type="text"><br />
Password:<input name="pass" type="text"><br />
ReEnter Password:<input name="confirm" type="text"><br />
    
    	<br />
    	 <input name="Submit1" type="submit" value="submit" /><br />

</form>

</body>
</html>

 

 

Link to comment
Share on other sites

try this slightly modified version of my last attempt:

   <body>
<?php

$Conn=mysql_connect("localhost","fierm","13183");

if(!$Conn){ 
   echo "failed";
  } 
  else{ 
mysql_select_db("fierm");


  $User=$_POST["user"];
  $Pass=$_POST["pass"];
  $Conf=$_POST["confirm"];

  	if($Pass == $Conf){ 
  		$result=mysql_query("SELECT * FROM user WHERE usr_name='$User' AND `password`='{$Pass}' LIMIT 1;") or die("It failed on the first query :".mysql_error());
  		$cou=mysql_num_rows($result);
  		if($cou == 1) 
    	{ 
    		echo "user already exists";  
  		}
  		else{ 
  			$result = mysql_query("SELECT * FROM user WHERE usr_name='{$User}' LIMIT 1;") or die("It failed on the second query :".mysql_error());
  			if (mysql_num_rows($result) != 1){
     			mysql_query("Insert into user (usr_name,pwd) Values ('$User','$Pass')");
    			echo "New User Was Added";
  			}
  		}
	}   
else{
    	echo "passwords do not match";
  	}	 
}     
?>
</body>   <body>
<?php

$Conn=mysql_connect("localhost","fierm","13183");

if(!$Conn){ 
   echo "failed";
  } 
  else{ 
mysql_select_db("fierm");


  $User=$_POST["user"];
  $Pass=$_POST["pass"];
  $Conf=$_POST["confirm"];

  	if($Pass == $Conf){ 
  		$result=mysql_query("SELECT * FROM user WHERE usr_name='$User' AND `password`='{$Pass}' LIMIT 1;") or die("It failed on the third query :".mysql_error());
  		$cou=mysql_num_rows($result);
  		if($cou == 1) 
    	{ 
    		echo "user already exists";  
  		}
  		else{ 
  			$result = mysql_query("SELECT * FROM user WHERE usr_name='{$User}' LIMIT 1;");
  			if (mysql_num_rows($result) != 1){
     			mysql_query("Insert into user (usr_name,pwd) Values ('$User','$Pass')");
    			echo "New User Was Added";
  			}
  		}
	}   
else{
    	echo "passwords do not match";
  	}	 
}     
?>
</body>

***EDITED to add error checking

Link to comment
Share on other sites

<body>
<?php

$Conn = mysql_connect('localhost', 'fierm', '13183');

if (!$Conn) {
    echo "failed";
} else {
    mysql_select_db('fierm', $Conn);


    $User = $_POST['user'];
    $Pass = $_POST['pass'];
    $Conf = $_POST['confirm'];

    if ($Pass == $Conf) {
        $result = mysql_query("SELECT * FROM `user` WHERE usr_name='" . $User . "'") or
            die(mysql_error());
        $cou = mysql_num_rows($result);
        if ($cou > 0) {
            echo "user already exists";
        } else {
            mysql_query("INSERT INTO `user` (`usr_name`,`pwd`) Values ('".$User."','".$Pass."')");
            echo "New User Was Added";
        }
    } else {
        echo "passwords do not match";
    }
}
?>
</body>

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.