![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Aug 2005
Location: Leeds - UK
Posts: 69
Rep Power: 4
![]() |
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? |
|
|
|
|
|
#2 |
|
Professional Programmer
|
You have :
while {
if{
}//end of if
}//end of while
else{}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 ! |
|
|
|
|
|
#3 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 850
Rep Power: 4
![]() |
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! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|