Skor Posted August 26, 2007 Share Posted August 26, 2007 This one's got me scratching my head. I've got a page which displays inventory which has worked fine for months. I'm in the process of adding a drop-down to allow visitors to view a subset of the inventory. The problem is once I've added the form, the original query doesn't execute upon display. I've tinkered with a couple of things, but am just baffled. Any insights will be appreciated. Form Section: <FORM NAME="sort" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <SELECT NAME="earcode" <OPTION VALUE=""></option> <OPTION VALUE="ALL" selected="selected">Display all Earrings </option> <OPTION VALUE="E">Display only Earwire Earrings</option> <OPTION VALUE="P">Display only Post Earrings</option> <OPTION VALUE="L">Display only Lever back Earrings</option> </SELECT> <input type="submit" value="Display Selected Earrings"> </FORM> A block that doesn't seem to make a difference: if (isset($_REQUEST['submitted']) && $_REQUEST['submitted'] == '1') { echo "Form submitted!"; } Here's the standard query: $query = "SELECT code, id,name,price,description FROM dbtable WHERE status = 'A' AND code = 'E'"; $result = mysql_query($query) or die('Query not successfully processed: ' . mysql_error()); Here's the logic block from the form. If I comment this out, the page query executes just fine. // select dbquery if ($earcode =="ALL") $queryb = "SELECT code, id,name,price,description FROM dbtable WHERE status = 'A' AND code = 'E'"; else $queryb = "SELECT code,id,name,price,description FROM dbtable WHERE status = 'A' AND cat_code = 'E' AND ear_code = '$earcode'" ; $result = mysql_query($queryb) or die('Query not successfully processed: ' . mysql_error()); Here's another block of code, pretty standard stuff: $cols = mysql_num_fields( $result ); // get each row while($row = mysql_fetch_row($result)) { What am I missing here? Do I need completely separate paths? Are they tripping over shared variables? Do I need another branch on my IF? Thanks in advance. Quote Link to comment Share on other sites More sharing options...
matthewhaworth Posted August 26, 2007 Share Posted August 26, 2007 <FORM NAME="sort" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <SELECT NAME="earcode" <OPTION VALUE=""></option> <OPTION VALUE="ALL" selected="selected">Display all Earrings </option> <OPTION VALUE="E">Display only Earwire Earrings</option> <OPTION VALUE="P">Display only Post Earrings</option> <OPTION VALUE="L">Display only Lever back Earrings</option> </SELECT> <input type="submit" value="Display Selected Earrings"> </FORM> You're missing a '>' here: <SELECT NAME="earcode" . That's what lol Quote Link to comment Share on other sites More sharing options...
Skor Posted August 26, 2007 Author Share Posted August 26, 2007 That wasn't it. Actually the form works fine, it's the initial inventory pull that doesn't work. Thanks for closing my tag. Quote Link to comment Share on other sites More sharing options...
matthewhaworth Posted August 26, 2007 Share Posted August 26, 2007 That wasn't it. Actually the form works fine, it's the initial inventory pull that doesn't work. Thanks for closing my tag. Do you get an error message? If so, what? $queryb = "SELECT code,id,name,price,description FROM dbtable WHERE status = 'A' AND cat_code = 'E' AND ear_code = '$earcode'" ; Space between end of the line and semicolon. Infact.. if ($earcode == "ALL") { $queryb = "SELECT code, id,name,price,description FROM dbtable WHERE status = 'A' AND code = 'E';"; } else { $queryb = "SELECT code,id,name,price,description FROM dbtable WHERE status = 'A' AND cat_code = 'E' AND ear_code = '".$earcode."';"; } $result = mysql_query($queryb) or die('Query not successfully processed: ' . mysql_error()); Always use {. Quote Link to comment Share on other sites More sharing options...
trq Posted August 26, 2007 Share Posted August 26, 2007 You might want to post your actual code, from what I can tell your using allot of undefined variables. Here... if (isset($_REQUEST['submitted']) && $_REQUEST['submitted'] == '1') { echo "Form submitted!"; } You check $_REQUEST['submitted'], yet knowhere in your form do you have an element named submitted. Here... if ($earcode =="ALL") You use the variable $earcode, yet we don't see where you define it. Post your actual code instead of snippets! Quote Link to comment Share on other sites More sharing options...
Skor Posted August 26, 2007 Author Share Posted August 26, 2007 Last time I posted the whole code I got chastised. Here's the variable definition: $earcode = $_POST['earcode']; Quote Link to comment Share on other sites More sharing options...
matthewhaworth Posted August 26, 2007 Share Posted August 26, 2007 Last time I posted the whole code I got chastised. Here's the variable definition: $earcode = $_POST['earcode']; Well a moderator just asked you to do it, so you won't this time. Quote Link to comment Share on other sites More sharing options...
trq Posted August 26, 2007 Share Posted August 26, 2007 Of course we only need the relevent parts, but we need enough to be able to see your problem. Quote Link to comment Share on other sites More sharing options...
Skor Posted August 26, 2007 Author Share Posted August 26, 2007 I didn't leave out much the first time. To reitierate the first query is not executing upon page load, but the form works fine. If I comment out the if clause, the page loads fine, but obviously the form doesn't do anything. My gut tells me there's something with the IF that's confusing the the first query from being executed. Is the $result getting confused? $earcode = $_POST['earcode']; <FORM NAME="sort" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <SELECT NAME="earcode" > <OPTION VALUE=""></option> <OPTION VALUE="ALL" selected="selected">Display all Earrings </option> <OPTION VALUE="E">Display only Earwire Earrings</option> <OPTION VALUE="P">Display only Post Earrings</option> <OPTION VALUE="L">Display only Lever back Earrings</option> </SELECT> <input type="submit" value="Display Selected Earrings"> </FORM> <?php //setting the stage mysql_connect($host, $user, $password) or die ("Unable to connect to server"); mysql_select_db($db_name) or die ("Couldn't retrieve database information"); $query = "SELECT code, id,name,price,description FROM dbtable WHERE status = 'A' AND code = 'E'"; $result = mysql_query($query) or die('Query not successfully processed: ' . mysql_error()); // select dbquery if ($earcode =="ALL") { $queryb = SELECT code, id,name,price,description FROM dbtable WHERE status = 'A' AND code = 'E'"; $result = mysql_query($queryb) or die('Query not successfully processed: ' . mysql_error()); } else { $queryb = "SELECT code,id,name,price,description FROM dbtable WHERE status = 'A' AND cat_code = 'E' AND ear_code = '$earcode'" ; $result = mysql_query($queryb) or die('Query not successfully processed: ' . mysql_error()); } $cols = mysql_num_fields( $result ); // get each row while($row = mysql_fetch_row($result)) { $Code = $row[0]; $ID = $row[1]; $Name = $row[2]; $Price = $row[3]; $Desc = $row[4]; Quote Link to comment Share on other sites More sharing options...
matthewhaworth Posted August 26, 2007 Share Posted August 26, 2007 $queryb = SELECT code, id,name,price,description FROM dbtable WHERE status = 'A' AND code = 'E'"; Needs ".. but i know that's not the problem Also, why isn't $earcode = $_POST['earcode']; in php tags? Quote Link to comment Share on other sites More sharing options...
trq Posted August 26, 2007 Share Posted August 26, 2007 Your logic is all over the place. You only need to form your queries based on what is selected, you don't have to execute them all. As an example, Iwould do something like.... <?php // display form if (isset($_POST['submit'])) { switch ($_POST['earcode']) { case 'A': $sql = "QUERY RELATED TO A"; break; case 'B': $sql = "QUERY RELATED TO B"; break; default: $sql = "YOUR DEFAULT QUERY"; } } else { $sql = "YOUR DEFAULT QUERY"; } if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result)) { // display data here. } } } ?> Quote Link to comment Share on other sites More sharing options...
Skor Posted August 28, 2007 Author Share Posted August 28, 2007 I had to do some reading on the switch function. I've incorporated it and it seems to ignore all but the default query. In other words, both the link to the page and clicking the submit button after the making a selection in the drop down menu generate the same results. Thanks in advance for any code suggestions. <?php if (isset($_POST['submit'])) { switch ($_POST['earcode']) { case 'All': if ($earcode == "ALL") { $query = "SELECT cat_code,id,name,price,description FROM dbtable WHERE status = 'A' AND cat_code = 'E' order by pid DESC "; } break; case 'Type': if ($earcode == 'E' || $earcode == 'P' || $earcode == 'L') { $query = "SELECT cat_code,id,name,price,description FROM dbtable WHERE status = 'A' AND cat_code = 'E' AND ear_code = '$earcode' order by pid DESC " ; } break; default: $query = "SELECT cat_code,id,name,price,description FROM dbtable WHERE status = 'A' AND cat_code = 'E' order by pid DESC "; } } else { $query = "SELECT cat_code,id,name,price,description FROM dbtable WHERE status = 'A' AND cat_code = 'E' order by pid DESC " ; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.