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> Link to comment https://forums.phpfreaks.com/topic/101463-problem-with-if-and-else/ 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. Link to comment https://forums.phpfreaks.com/topic/101463-problem-with-if-and-else/#findComment-519014 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. Link to comment https://forums.phpfreaks.com/topic/101463-problem-with-if-and-else/#findComment-519016 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()); Link to comment https://forums.phpfreaks.com/topic/101463-problem-with-if-and-else/#findComment-519019 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> Link to comment https://forums.phpfreaks.com/topic/101463-problem-with-if-and-else/#findComment-519020 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 Link to comment https://forums.phpfreaks.com/topic/101463-problem-with-if-and-else/#findComment-519026 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. Link to comment https://forums.phpfreaks.com/topic/101463-problem-with-if-and-else/#findComment-519028 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" Link to comment https://forums.phpfreaks.com/topic/101463-problem-with-if-and-else/#findComment-519029 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> Link to comment https://forums.phpfreaks.com/topic/101463-problem-with-if-and-else/#findComment-519031 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 Link to comment https://forums.phpfreaks.com/topic/101463-problem-with-if-and-else/#findComment-519032 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 Link to comment https://forums.phpfreaks.com/topic/101463-problem-with-if-and-else/#findComment-519034 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> Link to comment https://forums.phpfreaks.com/topic/101463-problem-with-if-and-else/#findComment-519035 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 Link to comment https://forums.phpfreaks.com/topic/101463-problem-with-if-and-else/#findComment-519037 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 Link to comment https://forums.phpfreaks.com/topic/101463-problem-with-if-and-else/#findComment-519050 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. Link to comment https://forums.phpfreaks.com/topic/101463-problem-with-if-and-else/#findComment-519051 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 Link to comment https://forums.phpfreaks.com/topic/101463-problem-with-if-and-else/#findComment-519052 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.