Page 1 of 1

if statement being weird

Posted: Thu Jun 12, 2008 1:30 pm
by relief8
To see the detail page you can click on the link or type in the style number in the search form. If you type in the style number in the search form everything works great but if you click on a link you get this error at the top of the page:

Notice: Undefined index: fromSearch in C:\webserver\Apache2\htdocs\pacificHeadwear2008\product_detail.php on line 8

but everything still shows up correctly below that. Can anyone tell me why this is happening. Here is the code that is breaking starting at line 8:

Code: Select all

 
if($_REQUEST['fromSearch'] != "") {
        mysql_select_db($database_dbConn, $dbConn);
        $query_rsProducts = "SELECT products.id as pid, series.name as series, products.model as model, products.name as name,
                                    products.is_New as new, products.profile_material as profile, products.crown, products.visor,
                                    products.sweatband as sweatband, products.sizes as sizes, products.closure as closure,
                                    products.unit_price as price, products.notes
                                FROM products JOIN series ON series.id = products.series_id
                                    WHERE products.model LIKE '".$_REQUEST['fromSearch']."%'";
        $rsProducts = mysql_query($query_rsProducts, $dbConn) or die(mysql_error());
        $row_rsProducts = mysql_fetch_assoc($rsProducts);
        $totalRows_rsProducts = mysql_num_rows($rsProducts);
        $pid = $row_rsProducts['pid'];
    } else {
        mysql_select_db($database_dbConn, $dbConn);
        $query_rsProducts = "SELECT products.id as pid, series.id as sid, series.name as series, products.model as model, products.name as name,
                                    products.is_New as new, products.profile_material as profile, products.crown, products.visor,
                                    products.sweatband as sweatband, products.sizes as sizes, products.closure as closure,
                                    products.unit_price as price, products.notes
                                FROM products JOIN series ON series.id = products.series_id
                                    WHERE products.id = '".$_REQUEST['pid']."'";
        $rsProducts = mysql_query($query_rsProducts, $dbConn) or die(mysql_error());
        $row_rsProducts = mysql_fetch_assoc($rsProducts);
        $totalRows_rsProducts = mysql_num_rows($rsProducts);
        $pid = $row_rsProducts['pid'];
    }
 

Re: if statement being weird

Posted: Thu Jun 12, 2008 2:10 pm
by orbitz
I think you could use:
// Report all errors except E_NOTICE
error_reporting(E_ALL ^ E_NOTICE);

and / or use empty() to check if the variable is set.

Re: if statement being weird

Posted: Thu Jun 12, 2008 2:27 pm
by relief8
Hmmm, nothing. I wonder if my question isn't clear enough.

When someone types a style number in the search form the search form id is fromSearch. So the if statement checks to see if that has been set and if not it should skip down to the else section. For some reason it's giving me that error like it needs that variable but it shouldn't, it should just skip down to the else section.

Re: if statement being weird

Posted: Thu Jun 12, 2008 2:38 pm
by Weirdan
So the if statement checks to see if that has been set
And thus you have to use isset() for that.

Re: if statement being weird

Posted: Thu Jun 12, 2008 3:03 pm
by relief8
I tried this.

Code: Select all

 
if (isset($fromSearch)) {
 
And it didn't work. This is the code for the form:

Code: Select all

 
 <form action="product_detail.php" method="get" accept-charset="utf-8" />
         Search:
         <input type="text" name="fromSearch" value="Enter Style Number..." id="fromSearch" size="20" maxlength="20" tabindex="0" onclick="SelectAll('fromSearch');"/>
         <input type="submit" value="Go" />   
         </form> 
 

Re: if statement being weird

Posted: Thu Jun 12, 2008 4:37 pm
by Weirdan

Code: Select all

 
if (isset($_REQUEST['fromSearch'])) {
  // do something
}
 

Re: if statement being weird

Posted: Thu Jun 12, 2008 4:48 pm
by relief8
Perfect. Thank you so much. That was causing me a huge headache. I appreciate the help.