gammaman Posted April 17, 2008 Share Posted April 17, 2008 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> Quote Link to comment Share on other sites More sharing options...
marcus Posted April 17, 2008 Share Posted April 17, 2008 Confirm that you're passing the correct POST variables. Your code looks correct, but the form may have an issue. Quote Link to comment Share on other sites More sharing options...
gammaman Posted April 17, 2008 Author Share Posted April 17, 2008 Yes Iam using POST, like I said all the other parts work. It only does not work when I enter an existing user with an existing user name and password. Quote Link to comment Share on other sites More sharing options...
marcus Posted April 17, 2008 Share Posted April 17, 2008 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()); Quote Link to comment Share on other sites More sharing options...
jonsjava Posted April 17, 2008 Share Posted April 17, 2008 <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> Quote Link to comment Share on other sites More sharing options...
gammaman Posted April 17, 2008 Author Share Posted April 17, 2008 Still does not work if the user already exists. I tried to echo $cou but nothing was displayed so it is possible that it is automatically falling through to the else Quote Link to comment Share on other sites More sharing options...
marcus Posted April 17, 2008 Share Posted April 17, 2008 Show us your FULL code, even the form. Quote Link to comment Share on other sites More sharing options...
jonsjava Posted April 17, 2008 Share Posted April 17, 2008 I agree. I'm betting that your variables are not named "pass" and "confirm" Quote Link to comment Share on other sites More sharing options...
gammaman Posted April 17, 2008 Author Share Posted April 17, 2008 <!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> Quote Link to comment Share on other sites More sharing options...
gammaman Posted April 17, 2008 Author Share Posted April 17, 2008 Oh and by the way it is on two seperate pages Quote Link to comment Share on other sites More sharing options...
jonsjava Posted April 17, 2008 Share Posted April 17, 2008 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 Quote Link to comment Share on other sites More sharing options...
marcus Posted April 17, 2008 Share Posted April 17, 2008 <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> Quote Link to comment Share on other sites More sharing options...
gammaman Posted April 17, 2008 Author Share Posted April 17, 2008 That does not work either, now it does not display anything when the user already exists. The other parts work fine. Thanks for your help Quote Link to comment Share on other sites More sharing options...
gammaman Posted April 17, 2008 Author Share Posted April 17, 2008 It works now, your code was good except that you put result=mysql_query("SELECT * FROM user WHERE usr_name='$User' AND `password`='{$Pass}' LIMIT the field name is pwd not password Thanks again, that was driving me crazy Quote Link to comment Share on other sites More sharing options...
marcus Posted April 17, 2008 Share Posted April 17, 2008 You do realize the possibilities of a user creating an account with the same username and password are very slim. You only need to check the username. Quote Link to comment Share on other sites More sharing options...
gammaman Posted April 17, 2008 Author Share Posted April 17, 2008 Yes that is true but it does not hurt to check both 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.