greencoin Posted June 7, 2007 Share Posted June 7, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/54662-solved-php-newbie-trying-to-make-populated-drop-down-box-query/ Share on other sites More sharing options...
trq Posted June 7, 2007 Share Posted June 7, 2007 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"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/54662-solved-php-newbie-trying-to-make-populated-drop-down-box-query/#findComment-270368 Share on other sites More sharing options...
greencoin Posted June 8, 2007 Author Share Posted June 8, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/54662-solved-php-newbie-trying-to-make-populated-drop-down-box-query/#findComment-270444 Share on other sites More sharing options...
trq Posted June 8, 2007 Share Posted June 8, 2007 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"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/54662-solved-php-newbie-trying-to-make-populated-drop-down-box-query/#findComment-270449 Share on other sites More sharing options...
greencoin Posted June 8, 2007 Author Share Posted June 8, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/54662-solved-php-newbie-trying-to-make-populated-drop-down-box-query/#findComment-270458 Share on other sites More sharing options...
trq Posted June 8, 2007 Share Posted June 8, 2007 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']}'"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/54662-solved-php-newbie-trying-to-make-populated-drop-down-box-query/#findComment-270460 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.