Jump to content

[SOLVED] php newbie - trying to make populated drop down box query


Recommended Posts

My host is Godaddy, sql 4.1 - trying to create a populated drop down list that when submitted, displays the info row by row. I can populate the list, and when I post it takes me to the next page. The php on the next page makes the table header but there is no data underneath.

 

There is data in the table cause I can display it all on another php page I have.

Some of my fields are "customer", "phone", "city"...

 

Obviously the mysql connect info has been removed...

 

page1 code:

****************************************

$query = "SELECT cid FROM `GC_Tracker`"; 

$result = mysql_query($query) 

 

or die(mysql_error()); 

?>

Select<form action="track_results.php" method="post"> 

  <select size="1" name="invoice"> 

<?

while(list($name)= mysql_fetch_row($result)) 

 

?> 

 

  <option value="<? echo "$name;" ?>"><?php echo "$name"; ?></option> 

 

<?php 

?> 

</select><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p> 

</form>

**********************************************************************************

page2 code:

**********************************************************************************

$sql = 'SELECT * FROM GC_Tracker WHERE cid = "'.$_POST['invoice'].'"';

$result = mysql_query($sql) or die(mysql_error());

 

echo "<TABLE BORDER=\"1\">\n";

echo "<TR bgcolor=\"lightblue\"><TD>Company</TD><TD>Phone</TD><TD>City</TD></TR>\n";

for($i = 0; $i < $numofrows; $i++) {

    $row = mysql_fetch_array($result);

    if($i % 2) {

        echo "<TR bgcolor=\"yellow\">\n";

    } else {

        echo "<TR bgcolor=\"white\">\n";

    }

    echo "<TD>".$row['customer']."</TD><TD>".$row['phone']."</TD><TD>".$row['city']."</TD>\n";

    echo "</TR>\n";

}

 

echo "</TABLE>\n";

****************************************************************************

 

Been racking my brain and now it hurts - any help is MUCH appreciated. Thanks ~Rich

You haven't defined $numofrows anywhere, so your loop isn't working. Try....

 

<?php

  $sql = 'SELECT * FROM GC_Tracker WHERE cid = "'.$_POST['invoice'].'"';
  if ($result = mysql_query($sql) && mysql_num_rows($result)) {
    $i = 0;
    echo "<TABLE BORDER=\"1\">\n";
    echo "<TR bgcolor=\"lightblue\"><TD>Company</TD><TD>Phone</TD><TD>City</TD></TR>\n";
    while ($row = mysql_fetch_array($result)) {
      if ($i % 2) {
        echo "<TR bgcolor=\"yellow\">\n";
      } else {
        echo "<TR bgcolor=\"white\">\n";
      }
      echo "<TD>".$row['customer']."</TD><TD>".$row['phone']."</TD><TD>".$row['city']."</TD>\n";
      echo "</TR>\n";
      $i++;
    }
  }
  echo "</TABLE>\n";

?>

nope - sorry... here's what I get:

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/content/f/t/p/ftpgreencoin/html/rich/backend/track_results.php on line 5

 

I copied your content verbatum with the exception of adding my database connection code. ~Rich

That means your query is failing. Try this....

 

<?php

  if (isset($_POST['invoice'])) {
    $sql = 'SELECT * FROM GC_Tracker WHERE cid = "'.$_POST['invoice'].'"';
    if ($result = mysql_query($sql)) {
      if (mysql_num_rows($result)) {
        $i = 0;
        echo "<TABLE BORDER=\"1\">\n";
        echo "<TR bgcolor=\"lightblue\"><TD>Company</TD><TD>Phone</TD><TD>City</TD></TR>\n";
        while ($row = mysql_fetch_array($result)) {
          if ($i % 2) {
            echo "<TR bgcolor=\"yellow\">\n";
          } else {
            echo "<TR bgcolor=\"white\">\n";
          }
          echo "<TD>".$row['customer']."</TD><TD>".$row['phone']."</TD><TD>".$row['city']."</TD>\n";
          echo "</TR>\n";
          $i++;
        }
        echo "</TABLE>\n";
      } else {
        echo "No results found";
      }
    } else {
      echo "Query failed<br />" . $sql . "<br />" . mysql_error();
    }
  } else {
    echo "form not submitted";
  }

?>

Very nice - looks like it's workin fine. Many, many, many, many, many thanks.

 

It's late in my time zone - I'll study the difference in the codes tomorrow and see if I can figure out where I went wrong.

 

btw, on the same table I need to make a query that is constrained by a user selected range of numbers / dates (bolean?). Example, the user can choose a range of invoices from 2 dropdowns for start date and end date. Any advice where I can find some info / complete code to see how to do it? ~Rich

btw, on the same table I need to make a query that is constrained by a user selected range of numbers / dates (bolean?). Example, the user can choose a range of invoices from 2 dropdowns for start date and end date. Any advice where I can find some info / complete code to see how to do it? ~Rich

 

Not hard (and nothing to do with booleans). Example.

 

<?php

  if (isset($_POST['min']) && isset($_POST['max']) { // where min and max are the names of the two form fields.
    $sql = "SELECT foo FROM bar WHERE fld >= '{$_POST['min']}' && fld <= '{$_POST['max']}'";
  }

?>

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.