Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 15th, 2006, 12:45 PM   #1
k4pil
Programmer
 
Join Date: Aug 2005
Location: Leeds - UK
Posts: 69
Rep Power: 4 k4pil is on a distinguished road
Logic error

Hi there.

I am creating a user account for my site. Each user has a username and password. I am trying to check whether the username or password entered already exists, if it does then an error is displayed else data is added to the database.

However, im not able to get the logic sorted.

This is the part of the code i can't suss...eerything else e.g. database conncection and query is working fine.

include '../connect.php';

$query = "SELECT * FROM staff";
$result = mysql_query($query); 
$num = mysql_numrows($result);
$i=0;
while ($i < $num)
{
$usernameD=mysql_result($result,$i,"username");
$passwordD=mysql_result($result,$i,"password");

if ($usernameD == $Uusername || $Upassword == $password)
{
	echo "Details already exists. Please choose a different details";
	$value = "1";
	$i=$num;
}

$i++;
}


else
{
$query = "INSERT INTO staff VALUES ('$department', '$access' , '$Uusername' , '$Upassword' , '$name' , '$surname')";
$result = mysql_query($query); 

echo "<p>Data has been entered!!!";
}


I know its the else/while/if combo which isnt spot on, but im stuck.

Any help?
k4pil is offline   Reply With Quote
Old Feb 15th, 2006, 1:26 PM   #2
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 381
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
You have :
while {
     if{
    }//end of if
}//end of while
else{}
else should be after if.
Another way you could do what you want is to

SELECT COUNT(*) FROM staff
WHERE staff.username == '$Uusername'

Then , if the query returns a number larger than 0, it means the user allready exists.

The sql might have mistakes , but you get the point. There's no reason to check if the password exists since you don't want 2 identical usernames in the first place
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Old Feb 15th, 2006, 6:12 PM   #3
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 825
Rep Power: 4 The Dark is on a distinguished road
You don't want to put the else after the if in the loop, otherwise you will only be checking the first username!

As xavier said, limiting your SQL is a good idea, if you still want to go with the loop, use the value of $value as a test to see if the user name/password was found. You need to set $value to something other than 1 before the loop and the instead of "else" you could say
  if ($value != "1")

I would probaly call it something other than $value, maybe $userExists.

Also checking for username OR password seems odd - you have just created an easy way for someone to see if a particular password is in use by anyone!
The Dark is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:38 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC