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
https://forums.phpfreaks.com/topic/101463-problem-with-if-and-else/
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>

<!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>

 

 

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.