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
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]
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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