Jump to content

else if problems


dad00

Recommended Posts

why wont this code work

 

if(strlen($username) == 0 || strlen($password) == 0)
{
        echo "Error please try again";
}
else if($rows['username'] == $username)
{
        echo "Error username already in use";
}
else if($rows['email'] == $email)
{
      echo "Error email address already in use";
}
else
{
     mysql_query("INSERT INTO users (username,password,email) VALUES ('$username','$password','$email')");
     echo "Register Succesfull";
}
}

Link to comment
https://forums.phpfreaks.com/topic/147444-else-if-problems/
Share on other sites

What doesn't work?

 

Why not check for the existence of $username or $email with a SQL query:

 

$result=mysql_query(SELECT * FROM users WHERE username='$username');
$num_records=mysql_num_rows($result);

 

If $numrecords is greater than 0 then username in use.

 

Not sure how you are doing it.

Link to comment
https://forums.phpfreaks.com/topic/147444-else-if-problems/#findComment-773933
Share on other sites

the actual code itself dosent work i can use the same username as 1 that already exists all my code:

 

if(isset($_POST['login']))
{ 
$sql = "SELECT * FROM users";
$sql = mysql_query($sql) or die(mysql_error());
//print_r($sql);
while($rows = mysql_fetch_assoc($sql))
{
$usernamec = $rows['username'];
$emailc = $rows['email'];
}
$username = trim($_POST['username']); //gets the username from the text box 'username' and removes any blank space
$password =trim($_POST['password']); //same as above just for password
$email =trim($_POST['email']);
if(strlen($username) == 0 || strlen($password) == 0)
{
        echo "Error please try again";
}
else if($rows['username'] == $username)
{
        echo "Error username already in use";
}
else if($rows['email'] == $email)
{
      echo "Error email address already in use";
}
else
{
     mysql_query("INSERT INTO users (username,password,email) VALUES ('$username','$password','$email')");
     echo "Register Succesfull";
}
}

Link to comment
https://forums.phpfreaks.com/topic/147444-else-if-problems/#findComment-774000
Share on other sites

$rows['username'] is updated everytime the while loop runs bringing in the next row from the database.

 

So you are effectively just checking against the last line of the database not all of them.  You can either put the error checking within the while loop or (better in my opinion) lose the while loop and do the error checking the way I suggested.

 

Oh and "Succesfull" is spelt "Successful" - sorry to be petty...  ;)

Link to comment
https://forums.phpfreaks.com/topic/147444-else-if-problems/#findComment-774010
Share on other sites

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.