Jump to content

Proper use IF and ELSE


gazalec

Recommended Posts

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
Link to comment
https://forums.phpfreaks.com/topic/35542-proper-use-if-and-else/
Share on other sites

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

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.