By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,614 Members | 1,653 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,614 IT Pros & Developers. It's quick & easy.

What is wrong with this SQL statement?

P: 64
The SQL statements are not working. It returns a page with only titles and no database results..
First file
Expand|Select|Wrap|Line Numbers
  1. <?    
  2. require_once '../../functions.php';
  3. ?>
  4. Select category to print
  5. <hr>
  6. <form name="frmPrint" action="printstocksheet.php">
  7. <table width="100%" border="0" align="center">
  8.           <tr>
  9.             <td width="128">
  10.             <select name="Category">
  11.                 <option value="0">Select Category</option>
  12.                 <?
  13.             $sql = "SELECT DISTINCT Category FROM cstockitems ORDER BY Category ASC";
  14.             $result = dbQuery($sql);        
  15.  
  16.             if(dbNumRows($result))
  17.             {
  18.                 while($row = dbFetchAssoc($result))
  19.                 {
  20.                 echo "<option>$row[Category]</option>";
  21.                 }
  22.             }
  23.                      else 
  24.                     {
  25.                 echo "<option>No Categories Present</option>"; 
  26.                     }
  27.       ?>
  28.               </select></td>
  29.             <td width="31"><input type="submit" name="submit" value="Go" class="button_image" onClick="return CheckShowBrandByCategory();"></td>
  30.             <td>
  31.               <input type="submit" name="submit" id="button" value="Back" onClick="window.location.href='index.php';" class="button_image">
  32.             </td>
  33.           </tr>
  34.         </table>
  35. </form>
  36.  
  37.  
  38.  
Second file

Expand|Select|Wrap|Line Numbers
  1. <?
  2.     require_once '../../functions.php';
  3.  
  4. ?>
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  6. <html xmlns="http://www.w3.org/1209/xhtml">
  7. <head>
  8. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  9. <title>STOCK FORM</title>
  10. <script language="JavaScript">
  11. var gAutoPrint = true; // Flag for whether or not to automatically call the print function
  12.  
  13. function printSpecial()
  14. {
  15.     if (document.getElementById != null)
  16.     {
  17.         var html = '<HTML>\n<HEAD>\n';
  18.  
  19.         if (document.getElementsByTagName != null)
  20.         {
  21.             var headTags = document.getElementsByTagName("head");
  22.             if (headTags.length > 0)
  23.                 html += headTags[0].innerHTML;
  24.         }
  25.  
  26.         html += '\n</HE' + 'AD>\n<BODY>\n';
  27.  
  28.         var printReadyElem = document.getElementById("printReady");
  29.  
  30.         if (printReadyElem != null)
  31.         {
  32.                 html += printReadyElem.innerHTML;
  33.         }
  34.         else
  35.         {
  36.             alert("Could not find the printReady section in the HTML");
  37.             return;
  38.         }
  39.  
  40.         html += '\n</BO' + 'DY>\n</HT' + 'ML>';
  41.  
  42.         var printWin = window.open("","printSpecial");
  43.         printWin.document.open();
  44.         printWin.document.write(html);
  45.         printWin.document.close();
  46.         if (gAutoPrint)
  47.             printWin.print();
  48.     }
  49.     else
  50.     {
  51.         alert("Sorry, the print ready feature is only available in modern browsers.");
  52.     }
  53. }
  54.  
  55. </script>
  56.  
  57. <style type="text/css">
  58.  
  59. .lines td {
  60. border: #666666 solid 1px;
  61. }
  62.  
  63. </style>
  64. </head>
  65.  
  66. <body>
  67.  
  68. <div id="printReady">
  69.  
  70. <table width="100%" border="0" class="lines">
  71.   <tr>
  72.     <td colspan="2"><div align="center"><strong></strong></div></td>
  73.     <?
  74.     $thismonth = mktime(0,0,0,date("m"),date("d"),date("Y"));
  75.  
  76.     $nextmonth = mktime(0,0,0,date("m")+1,date("d"),date("Y"));
  77.  
  78.     $montheafternext = mktime(0,0,0,date("m")+2,date("d"),date("Y"));
  79.  
  80.     ?>
  81.     <td colspan="5"><div align="center"><strong><? echo date("F", $thismonth); ?></strong></div></td>
  82.     <td colspan="5"><div align="center"><strong><? echo date("F", $nextmonth); ?></strong></div></td>
  83.     <td colspan="5"><div align="center"><strong><? echo date("F",$montheafternext); ?></strong></div></td>
  84.   </tr>
  85.   <tr align="center">
  86.     <td width="250"><em><strong>Brand</strong></em></td>
  87.     <td width="15"><em><strong>Qty</strong></em></td>
  88.     <td width="15">Day</td>
  89.     <td width="15">Sold</td>
  90.     <td width="15">Day</td>
  91.     <td width="15">Sold</td>
  92.     <td width="15"><em><strong>Bal</strong></em></td>
  93.     <td width="15">Day</td>
  94.     <td width="15">Sold</td>
  95.     <td width="15">Day</td>
  96.     <td width="15">Sold</td>
  97.     <td width="15"><em><strong>Bal</strong></em></td>
  98.     <td width="15">Day</td>
  99.     <td width="15">Sold</td>
  100.     <td width="15">Day</td>
  101.     <td width="15">Sold</td>
  102.     <td width="15"><em><strong>Bal</strong></em></td>
  103.     </tr>
  104.     <?
  105.  
  106.  
  107.     $Cat = '';
  108.  
  109.     if (isset($_POST['Category'])) $Cat = $_POST['Category'];
  110.  
  111.     $sqlTitle = "SELECT * FROM fragrancestock WHERE Category = '$Cat' ORDER BY Brand ASC";
  112.  
  113.     $result = dbQuery($sqlTitle);
  114.  
  115.     if (dbNumRows($result) > 0) {
  116.         $i = 0;
  117.  
  118.         while($row = dbFetchAssoc($result)) {
  119.             extract($row);
  120.  
  121.             if ($i%2) {
  122.                 $class = 'row1';
  123.             } else {
  124.                 $class = 'row2';
  125.             }
  126.  
  127.             $i += 1;
  128. ?>
  129.   <tr class="<?php echo $class; ?>">
  130.     <td width="250"><?php echo $Brand; ?></td>
  131.     <td width="15"><?php echo $Quantity; ?></td>
  132.     <td width="15">&nbsp;</td>
  133.     <td width="15">&nbsp;</td>
  134.     <td width="15">&nbsp;</td>
  135.     <td width="15">&nbsp;</td>
  136.     <td width="15">&nbsp;</td>
  137.     <td width="15">&nbsp;</td>
  138.     <td width="15">&nbsp;</td>
  139.     <td width="15">&nbsp;</td>
  140.     <td width="15">&nbsp;</td>
  141.     <td width="15">&nbsp;</td>
  142.     <td width="15">&nbsp;</td>
  143.     <td width="15">&nbsp;</td>
  144.     <td width="15">&nbsp;</td>
  145.     <td width="15">&nbsp;</td>
  146.     <td width="15">&nbsp;</td>
  147.     </tr>
  148.  
  149.      <?php
  150.     } // end while
  151.  
  152.  
  153. ?>
  154. </table>
  155. </div>
  156. <table width="100%" border="0">
  157.   <tr>
  158.     <td></td>
  159.   </tr>
  160.            <?php    
  161. } else {
  162. ?>
  163.   <tr>
  164.     <td>&nbsp;</td>
  165.   </tr>
  166.   <tr>
  167.   No stock items yet</td>
  168.        <?php
  169. }
  170. ?>
  171.     <td>&nbsp;</td>
  172.   </tr>
  173. </table>
  174.  
  175. <a href="javascript:void(printSpecial())">Print</a> 
  176. </body>
  177. </html>
  178.  
  179.  
When I remove the WHERE clause from the second file, it works well.
What is the problem here?
Oct 10 '08 #1
Share this Question
Share on Google+
1 Reply


hsriat
Expert 100+
P: 1,654
Do some reverse debugging like this:

Problem lies in
WHERE Category = '$Cat'
that means problem lies in
$Cat
that means problem lies in
$_POST['Category']
that means you are not doing any validation in case $_POST['Category'] is null
or problem lies in
$row[Category]

Now check out what's the problem.
Oct 10 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.