Jump to content

Error condition not triggering correct response


mmckimson1
Go to solution Solved by Barand,

Recommended Posts

First of all, I apologize for my "newbieness" in advance.  I've got a form processing script that is working fine except for one section of code, snippets of which is shown below:

          //default values
          $formResponse = "How are you really doing?  Use this to find out!";
          $imgSource = "blue.png";

				If ($country == "United States") {
						//US zip code query
						$sql = "SELECT location, revParData FROM zipData WHERE zipCode = '$zipCode' LIMIT 1";
						mysql_select_db('db');
						$retval = mysql_query( $sql, $conn );
						if(! $retval ) {
							$formResponse = "It looks like you entered an invalid zip code. Please try again!";
							$imgSource = "yellow.png";
						} else {
									$sql = "SELECT location, revParData FROM zipData WHERE zipCode = '$zipCode' LIMIT 1";
									mysql_select_db('db');
									$retval = mysql_query( $sql, $conn );

									while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
										$location = $row['location'];
										$revParData = $row['revParData'];
									}
						}

What I thought would happen is this:

 

  1. If the zip code entered by the end user does not exist in the table, the $formResponse and $imgSource values would be the "... invalid zip code" and "yellow.png" values.
  2. If a valid zip code is entered, it completes the second query.

In fact, it works fine if a zip code that exists in the table is entered, but shows the default values for $formResponse and $imgSource if a zip code that doesn't exist in the database is entered.

 

I can't figure out why it is doing this... if anyone can point out my coding error, it would be appreciated.

 

Cheers!

Mike

 

 

Link to comment
Share on other sites

 Wow!  This is a pretty incomplete section of code!

 

Here it is cleaned up a bit.

 

//default values
$formResponse = "How are you really doing?  Use this to find out!";
$imgSource = "blue.png";
If ($country == "United States")
{
    //US zip code query
    $sql = "SELECT location, revParData FROM zipData WHERE zipCode = '$zipCode' LIMIT 1";
    mysql_select_db('db');
    $retval = mysql_query( $sql, $conn );
    if(!$retval )
    {
        $formResponse = "It looks like you entered an invalid zip code. Please try again!";
        $imgSource = "yellow.png";
    }
    else
    {
        $sql = "SELECT location, revParData FROM zipData WHERE zipCode = '$zipCode' LIMIT 1";
        mysql_select_db('db');
       $retval = mysql_query( $sql, $conn );
       while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
       {
            $location = $row['location'];
            $revParData = $row['revParData'];
       }
   }
 

 

So incomplete.

 

As Barand said - no results is not the same as "!$results)".

 

Going thru this code I see that  you check to see if you are in the US and you do a query for a single zip code record.  Incorrectly you then output the 'invalid code' message if the query fails.  Skipping that you then re-run the exact same query if the query succeeded and once again output what you think is going to be a result.  Actually you need to not only check if the query ran (which you do) but also if it returned a result/row (which you don't.  Why you run the query twice - who know?

 

Furthermore - if you use the php manual you must have noticed that the MySQL_* functions are deprecated.  Use At Your Own Risk.  But since you are set in your ways here is how I would re-write it:

 

$formResponse = "How are you really doing?  Use this to find out!";
$imgSource = "blue.png";
mysql_select_db('db');
If ($country == "United States")
{
   $sql = "SELECT location, revParData FROM zipData WHERE zipCode = '$zipCode'";
   $retval = mysql_query( $sql, $conn );
   if(!$retval )
   {
       $formResponse = "Error running query - ".mysql_error();
       $imgSource = "yellow.png";
   }
   if ( mysql_num_rows() == 0)
   {
       $formResponse = "It looks like you entered an invalid zip code. Please try again!";
       $imgSource = "yellow.png";
   }
   else
   {
       $row = mysql_fetch_array($retval, MYSQL_ASSOC))
       $location = $row['location'];
       $revParData = $row['revParData'];
   }
...

 

No dupe query, no dupe select db, proper handling of all results.

 

Now stop using MySQL_*

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.