if statement being weird

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
relief8
Forum Newbie
Posts: 13
Joined: Fri Jan 25, 2008 4:28 pm

if statement being weird

Post 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'];
    }
 
orbitz
Forum Newbie
Posts: 3
Joined: Tue Jun 10, 2008 10:54 am

Re: if statement being weird

Post 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.
relief8
Forum Newbie
Posts: 13
Joined: Fri Jan 25, 2008 4:28 pm

Re: if statement being weird

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: if statement being weird

Post by Weirdan »

So the if statement checks to see if that has been set
And thus you have to use isset() for that.
relief8
Forum Newbie
Posts: 13
Joined: Fri Jan 25, 2008 4:28 pm

Re: if statement being weird

Post 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> 
 
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: if statement being weird

Post by Weirdan »

Code: Select all

 
if (isset($_REQUEST['fromSearch'])) {
  // do something
}
 
relief8
Forum Newbie
Posts: 13
Joined: Fri Jan 25, 2008 4:28 pm

Re: if statement being weird

Post by relief8 »

Perfect. Thank you so much. That was causing me a huge headache. I appreciate the help.
Post Reply