Hey Guys,

I haven't touched PHP in awhile so I am kinda rusty. I made a validation method within my new request invite class and need some help. I took out all the other code that works so we can focus on checking if email exists. I am trying to check my db table for any matching emails and if there are I put an error message into the $errors array. I have another method that checks if $errors is true and if so display errors within array. Well so far even if the email doesn't exist I get my "Whoa there! You already requested an invite!" error. What am I doing wrong? Any help would be greatly appreciated : )

PS: basic example so may look bare

public function isEmailAvailable(){

		   
	$query = "SELECT * FROM my_table WHERE email = '$this->email' LIMIT 1";

			$result = mysql_query($query);

			if (mysql_num_rows($result) > 0){
				$this->errors[] = 'Whoa there! You already requested an invite!';
			}


			  return count($this->errors)? 0 : 1;
	}

Hi,

I don't see anything wrong in your code.
However there is one small thing. I guess if there was already an email in the DB, then you wanted to return 1, else 0. But now it returns 0 if an email already exists, else 1. But my speculation is based only on the function name isEmailAvailable(). It might be that you really want to return data the way it is now. If so please try to echo the query and then try run it in phpmyadmin and check if it returns any data.

Thanks for the suggestions, I will try that and look into this further :icon_smile:

Hi,

I don't see anything wrong in your code.
However there is one small thing. I guess if there was already an email in the DB, then you wanted to return 1, else 0. But now it returns 0 if an email already exists, else 1. But my speculation is based only on the function name isEmailAvailable(). It might be that you really want to return data the way it is now. If so please try to echo the query and then try run it in phpmyadmin and check if it returns any data.

So when I echo the $result I get "Resource id #176"

Hi,

I don't see anything wrong in your code.
However there is one small thing. I guess if there was already an email in the DB, then you wanted to return 1, else 0. But now it returns 0 if an email already exists, else 1. But my speculation is based only on the function name isEmailAvailable(). It might be that you really want to return data the way it is now. If so please try to echo the query and then try run it in phpmyadmin and check if it returns any data.

Hi,

sorry for a bit late reply. I have added some debug info. Can you check what will be output you get for query and also the resultset ?

public function isEmailAvailable(){

           
    $query = "SELECT * FROM my_table WHERE email = '$this->email' LIMIT 1";

            $result = mysql_query($query);
            echo "Query - ".$query."<br />";
            while($row = mysql_fetch_assoc($result)) {
                echo "<pre>Result Row<br />";
                print_r($row);
                echo "</pre>";
            }
            if (mysql_num_rows($result) > 0){
                $this->errors[] = 'Whoa there! You already requested an invite!';
            }


              return count($this->errors)? 0 : 1;
    }

Thanks for the help here's what it output. Oh and the email was still inserted into the DB? I wonder if it always returns 1 because it is getting inserted some how.. anyways here's what I got back:

Query - SELECT * FROM my_table WHERE email = 'test@test.com' LIMIT 1
Result Row
Array
(
    [id] => 134
    [email] => test@test.com
    [city] => austin
    [requestDate] => 2011-11-22 08:22:43
)
The following errors have occurred!
- This email address already exists!

Can you post the code which calls for the email validation function and then inserts the record into the table ?
From the output you have posted above, it is obvious that there is already a record with that email in the table and hence the function mysql_num_rows($result) returns the value 1, thereby giving you the error message 'Whoa there! You already requested an invite!'

Sure so here's the methods that do that:

public function processRequest(){

	       if($this->isEmailAvailable()){
		     $this->submitInviteRequest();
               }
	}

	public function submitInviteRequest(){

	     //create new instance of the DB class and connect to DB
	     $condb = new DBCON();
	     $condb->startcon(); 

	$req_query = mysql_query("INSERT INTO my_table (requestDate, email, city) VALUES('$this->requestDate', '" . mysql_real_escape_string($this->email) . "', '" . mysql_real_escape_string($this->city) . "')");

		  if($req_query){

			echo "<div class=\"message_container\">";
			echo "<div class=\"message_top\"></div>";
			echo "<div class=\"message_mid\">";

			echo "<h1>Success</h1>";
			echo "<p>You have requested an invite!</p>";

				echo "</div>";
			echo "<div class=\"message_bottom\"></div>";
				echo "</div>";
		  }
		  else{

			  $this->errors[] = '- Sorry, your invite request has not been sent!';
		  }

	}

Can you post the code which calls for the email validation function and then inserts the record into the table ?
From the output you have posted above, it is obvious that there is already a record with that email in the table and hence the function mysql_num_rows($result) returns the value 1, thereby giving you the error message 'Whoa there! You already requested an invite!'

Even that looks fine to me :-). Can you check with the following changes in the function isEmailAvailable() ?

public function isEmailAvailable(){

           
    $query = "SELECT * FROM my_table WHERE email = '$this->email' LIMIT 1";

            $result = mysql_query($query);
            echo "Query - ".$query."<br />";
            while($row = mysql_fetch_assoc($result)) {
                echo "<pre>Result Row<br />";
                print_r($row);
                echo "</pre>";
            }
            if (mysql_num_rows($result) > 0){
                $this->errors[] = 'Whoa there! You already requested an invite!';
                echo "<br />Email already exists. Return False<br />";
                return false;
            } else {
                echo "<br />Email does not exists. Return True<br />";
                return true;
            }
    }

Thanks for all of your help, I figured it out recently. The code was fine, I created a WP plugin that did the same thing and it was still active...so the query was being inserted before the check. I forgot about that plugin, my bad!

Even that looks fine to me :-). Can you check with the following changes in the function isEmailAvailable() ?

public function isEmailAvailable(){

           
    $query = "SELECT * FROM my_table WHERE email = '$this->email' LIMIT 1";

            $result = mysql_query($query);
            echo "Query - ".$query."<br />";
            while($row = mysql_fetch_assoc($result)) {
                echo "<pre>Result Row<br />";
                print_r($row);
                echo "</pre>";
            }
            if (mysql_num_rows($result) > 0){
                $this->errors[] = 'Whoa there! You already requested an invite!';
                echo "<br />Email already exists. Return False<br />";
                return false;
            } else {
                echo "<br />Email does not exists. Return True<br />";
                return true;
            }
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.