gazalec Posted January 24, 2007 Share Posted January 24, 2007 hi i was just wondering if anyone could help me with a little problem, i have an admin part of a site where people with priviledges can add new customers/users and first the must check to see if a customer number/ username is in use and i was just wondering if this is the best way to do it.<?$cust=$_POST['cust'];$username="***";$password="******";$database="*****";$hostname="****";mysql_connect($hostname,$username,$password);mysql_select_db($database) or die( "Unable to select database");$query="SELECT * FROM *** where username = '".$cust."'";$result=mysql_query($query) or die(mysql_error());$num= mysql_numrows($result);mysql_close();$i=0;while ($i < $num) {$user=mysql_result($result,$i,"username");$i++;if ($user == 1){ echo $user."is a customer number and cannot be used<p>";or if ($user == 0) echo $user." is not a customer number in use<p>";}}?>if it is the only problem is my else clause doesn't work... if i put in a current customer number it gives the error message but if i put one that isn't in the database it displays nothing Quote Link to comment https://forums.phpfreaks.com/topic/35542-proper-use-if-and-else/ Share on other sites More sharing options...
redbullmarky Posted January 24, 2007 Share Posted January 24, 2007 erm i cant see an else clause ... ? you do need to change the or if ($user==0) line to else if ($user ==0) though Quote Link to comment https://forums.phpfreaks.com/topic/35542-proper-use-if-and-else/#findComment-168250 Share on other sites More sharing options...
The Little Guy Posted January 24, 2007 Share Posted January 24, 2007 Why don't you use sessions? Quote Link to comment https://forums.phpfreaks.com/topic/35542-proper-use-if-and-else/#findComment-168252 Share on other sites More sharing options...
mjlogan Posted January 24, 2007 Share Posted January 24, 2007 [quote author=gazalec link=topic=123856.msg512467#msg512467 date=1169659415][code=php:0]<?$cust=mysql_real_escape_string($_POST['cust']);// ....?>[/code][/quote]added security Quote Link to comment https://forums.phpfreaks.com/topic/35542-proper-use-if-and-else/#findComment-168253 Share on other sites More sharing options...
gazalec Posted January 24, 2007 Author Share Posted January 24, 2007 sorry i forgot i took it out that was wrong instead of 'or if' ??? there should be 'else' and how do i use sessions :-[ Quote Link to comment https://forums.phpfreaks.com/topic/35542-proper-use-if-and-else/#findComment-168254 Share on other sites More sharing options...
The Little Guy Posted January 24, 2007 Share Posted January 24, 2007 Looking over your code I realized that you don't need sessions for that part, but sessions are for logins and transfering data through multiple pages. If you want to do sessions here is a quicky:on a successfull login do this:[code=php:0]session_start(); #This line must come before anything is place in the source code.$_SESSION['username'] = $row['username']; #$row['username'] comes from the database$_SESSION['logged'] = 1; #This will be to check on other pages if they are logged in or not./*You can add as many of session variables as you would like.*/[/code]Next on all the pages where the user is logged in, you would use this:[code=php:0]session_start();if($_SESSION['logged']!=1){ header("Location: login.html"); #logged in will equal one if the user was successfully logged in}/*The above comes before anything is ouputed to the source code*/echo $_SESSION['username']; #This will print what is ever in your username session#The rest of your html follows[/code] Quote Link to comment https://forums.phpfreaks.com/topic/35542-proper-use-if-and-else/#findComment-168266 Share on other sites More sharing options...
gazalec Posted January 24, 2007 Author Share Posted January 24, 2007 ok kool thanks for that. so do u have any idea why my if and else isn't working? Quote Link to comment https://forums.phpfreaks.com/topic/35542-proper-use-if-and-else/#findComment-168276 Share on other sites More sharing options...
Jessica Posted January 24, 2007 Share Posted January 24, 2007 You have several syntax problems. Try this.[code]if ($user == 1){ echo $user." is a customer number and cannot be used<p>";}else if($user == 0){ echo $user." is not a customer number in use<p>";}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/35542-proper-use-if-and-else/#findComment-168282 Share on other sites More sharing options...
Psycho Posted January 24, 2007 Share Posted January 24, 2007 I think you are making this more complex than it needs to be. Plus you have a typo: it is mysql_num_rows (you are missing the 2nd underscore). How bout this:[code]<?$cust=$_POST['cust'];$username="***";$password="******";$database="*****";$hostname="****";mysql_connect($hostname,$username,$password);mysql_select_db($database) or die( "Unable to select database");$query="SELECT * FROM *** where username = '".$cust."'";$result=mysql_query($query) or die(mysql_error());if (mysql_num_rows($result) >= 1){ echo $user."is a customer number and cannot be used<p>";} else { echo $user." is not a customer number in use<p>";}mysql_close();}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/35542-proper-use-if-and-else/#findComment-168320 Share on other sites More sharing options...
gazalec Posted January 24, 2007 Author Share Posted January 24, 2007 thanks alot mjdamato it worked!! i only had to change one thing the $user in the echo command thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/35542-proper-use-if-and-else/#findComment-168377 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.