Jump to content

Page and Form Not Playing Well Together


Skor

Recommended Posts

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.

Link to comment
Share on other sites

<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

Link to comment
Share on other sites

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 {.

Link to comment
Share on other sites

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!

 

Link to comment
Share on other sites

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];

Link to comment
Share on other sites

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.
      }
    }
  }

?>

Link to comment
Share on other sites

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 " ;
  }

 

 

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.