Jump to content

!= worked for this but == didn't. Can someone please explain?


Benjigga

Recommended Posts

I'm making a registration page. I'm trying to get it to double check to see if an email address has already been submited. The == would let double email entries into the database. Here is the code that would let double entries in.

 

$query = "SELECT user_id from USERS where email='$e'";
$result = mysql_query($query);
if (mysql_num_rows($result) == 0) {

$query = "INSERT INTO users (first_name, last_name, email, password, registration_date) VALUES ('$fn', '$ln', '$e', SHA('p'), NOW() )";
$result = @mysql_query ($query); 
if ($result) { 

	echo '<h1 id="mainhead">Thank you!</h1>
	<p>You are now registered.</p><p><br /></p>';

	include ('./includes/footer.inc');
	exit();

} 

} else { 
echo '<h1 id="mainhead">Error!</h1>
<p class="error">The email address has already been registered.</p>';
}

 

Now here's what I changed it to and it stopped letting duplicate email addresses register. All I did was change == to != and switch the else statment with the if statement. Here it is.

 

$query = "SELECT user_id from USERS where email='$e'";
$result = mysql_query($query);
if (mysql_num_rows($result) != 0) {

$query = "INSERT INTO users (first_name, last_name, email, password, registration_date) VALUES ('$fn', '$ln', '$e', SHA('p'), NOW() )";
$result = @mysql_query ($query); 
if ($result) { 
	echo '<h1 id="mainhead">Error!</h1>
	<p class="error">The email address has already been registered.</p>';

} 

} else { 

echo '<h1 id="mainhead">Thank you!</h1>
<p>You are now registered.</p><p><br /></p>';

include ('./includes/footer.inc');
exit();

}

 

 

Can someone please explain to me why == wouldn't work but != did? I can't understand this.

 

Thank you much!

Thanks for the heads up. I understand != means Not Equal To and == means Equal To, I just don't understand why in this situation Not Equal To worked and Equal To didn't?

But if it works if you set ==, then obviously it won't work with !=. Something can't be both == and != simultaneously.

 

$query = "SELECT user_id from USERS where email='$e'";
$result = mysql_query($query);
if (mysql_num_rows($result) != 0) {
        echo '<h1 id="mainhead">Thank you!</h1>
<p>You are now registered.</p><p><br /></p>';

include ('./includes/footer.inc');
exit();
} else { 

        $query = "INSERT INTO users (first_name, last_name, email, password, registration_date) VALUES ('$fn', '$ln', '$e', SHA('p'), NOW() )";
$result = @mysql_query ($query); 
if ($result) { 
	echo '<h1 id="mainhead">Error!</h1>
	<p class="error">The email address has already been registered.</p>';

}

}

But if it works if you set ==, then obviously it won't work with !=. Something can't be both == and != simultaneously.

 

Example1:

if (sql == 0) {

Do 1

} else {

Do 2

}

 

In the first code, it was practically like the example above. In the second code, where I changed == to != I changed it to:

 

Example2:

if (sql != 0) {

Do 2

} else {

Do 1

}

 

Understand?

 

It worked with example 2, but not example 1. And I was just wondering if anyone could explain why?

But if it works if you set ==, then obviously it won't work with !=. Something can't be both == and != simultaneously.

 

Example1:

if  (sql == 0) {

Do 1

} else {

Do 2

}

 

In the first code, it was practically like the example above. In the second code, where I changed == to != I changed it to:

 

Example2:

if (sql != 0) {

Do 2

} else {

Do 1

}

 

Understand?

 

It worked with example 2, but not example 1. And I was just wondering if anyone could explain why?

Yes, but I don't see the change. Both codes look the same except you change == to !=. You didn't alternate "Do 1" and "Do 2".

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.