Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Displaying an SQL table from user inputs
Old 01-03-2009, 11:59 PM Displaying an SQL table from user inputs
Average Talker

Posts: 21
Name: Hal
Trades: 0
Hi all -

I am trying to display an SQL table using user inputs as the arguments for the SQL query. The problem is that the entire table is displayed on page load and when form button is pressed.

The page is: http://hoco.oxyhost.com/gps/test1.php

Thanks in advance for any help or input.


PHP Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head></head><body>

<?php include("connect.inc"); ?>

<form method ="post" action="<?php echo($_SERVER['PHP_SELF']); ?>"><div>&nbsp;

<div id="pnlSpecs" style="border-color:Maroon; border-width:1px;
             border-style:solid; height:160px; width:672px;
             text-align:center; z-index: 80; left: 40px;
             position: absolute; top: 40px; margin: 0px auto" >
    
<div id="pnlManufacturer" style="border-color:#804000; border-width:1px;
                      border-style:solid; height:72px; width:192px; z-index:82;
                      left: 177px; position: absolute; top: 8px">
        <span id="lblManufacturer" style="display:inline-block; height:16px; width:48px;
           z-index: 100; left: 4px; position: absolute; top: 8px">Manufacturer:</span>
                    <select name="make" id="ddlMake" style="width:80px;
                      z-index: 101;left: 104px; position: absolute; top: 8px">
                      <?php include("connect.inc");
                      
$makelist mysql_query("select distinct make from gpstable order by make")
                          or die(
"gpstable make query failed");
                      while (
$record_make =  mysql_fetch_array($makelist))
                        { 
printf("<option>%s</option>\n",$record_make['make']); } ?></select></div>

<div id="pnlResolution" style="border-color:Maroon;border-width:1px; border-style:solid;
                        height:72px; width:144px; z-index: 84; 
                        left: 512px; position: absolute; top: 8px">
                    <span id="lblResolution" style="display:inline-block; width:48px; z-index: 100;
                          left: 16px; position: absolute; top: 0px">Resolution:</span>
                <select name="resolutionmin" id="ddResMin" style="width:80px; 
                  z-index: 101; left: 60px; position: absolute; top: 24px">
                  <?php include("connect.inc");
                  
$resminlist mysql_query("select distinct res from gpstable order by res")
                          or die(
"gpstable res query failed");
                      while (
$record_resmin =  mysql_fetch_array($resminlist))
                      { 
printf("<option>%s</option>\n",$record_resmin['res']); } ?></select></div>
            
<div id="pnlPrice" style="border-color:Maroon; border-width:1px;
             border-style:solid; height:72px; width:160px; z-index: 81;
             left: 8px; position: absolute; top: 8px">
        
                <span id="lblPrice" style="z-index: 100; left: 16px;
                           position: absolute; top: 0px">Price:</span>
                <select name="pricemax" id="ddMaxPrice" style="width:88px;
                           z-index: 101; left:64px; position: absolute; top: 24px">
                       <?php include("connect.inc");
                      
$pricemaxlist mysql_query("select distinct price
                                        from gpstable order by price"
)
                          or die(
"gpstable price query failed");
                      while (
$record_pricemax =  mysql_fetch_array($pricemaxlist))
                       { 
printf("<option>%s</option>\n",$record_pricemax['price']); } ?></select></div>
<div id="pnlScreenSize" style="border-color:Maroon;border-width:1px; border-style:solid;
                      height:72px; width:128px; z-index: 83;
                      left: 376px; position: absolute; top: 8px">
        
                <span id="lblScreenSize" style="display:inline-block;width:80px;
                        z-index: 100; left: 8px; position: absolute;
                        top: 0px">Screen Size:</span>
            
                <select name="screensizemax" id="ddMaxScreenSize" style="width:80px;
                      z-index: 103; left: 44px; position: absolute; top: 24px">
                       <?php include("connect.inc");
                      
$screensizemaxlist mysql_query("select distinct scrsize
                                    from gpstable order by scrsize"
)
                          or die(
"gpstable screensize query failed");
                      while (
$record_screensizemax =  mysql_fetch_array($screensizemaxlist))
                        { 
printf("<option>%s</option>\n",$record_screensizemax['scrsize']); }
                             
?></select>&nbsp; </div>
</div>

<input type="submit" name="btnSearch" value="Submit" id="btnSearch" style="font-size:Medium;
              font-weight:bold;height:40px; width:144px;z-index: 100;
             left: 424px; position: absolute; top: 150px" />
           <br /><br /><br /><br /><br /><br />
</div>

<?php include("connect.inc"); ?>
<?php error_reporting
(E_ALL & ~E_NOTICE); ?>

<?php
// Table 
// Build the query, start with conditions from submitted input:
if (isset($_POST['querysubmit']))

// Get specified info from submitted input
  
$FilterId $_POST['id'];
  
$FilterPic $_POST['pic'];
  
$FilterMake $_POST['Make'];
  
$FilterScrSize $_POST['scrSize'];
  
$FilterRes $_POST['res'];
  
$FilterPrice $_POST['price'];

// Add row and rack values as conditions to the query
  
if (!empty($FilterId))
    {  
$FilterIdQuery "AND id =" $FilterId;
       
$FilterSet TRUE;    }
  if (!empty(
$FilterPic))
    {  
$FilterPicQuery "AND pic =" $FilterPic;
       
$FilterSet TRUE;    }
  if (!empty(
$FilterMake))
    {  
$FilterMakeQuery "AND make =" $FilterMake;
       
$FilterSet TRUE;    }
  if (!empty(
$FilterScrSize))
    {  
$FilterScrSizeQuery "AND scrSize =" $FilterScrSize;
       
$FilterSet TRUE;    }
  if (!empty(
$FilterRes))
    {  
$FilterResQuery "AND res =" $FilterRes;
       
$FilterSet TRUE;    }
  if (!empty(
$FilterPrice))
    {  
$FilterPriceQuery "AND price =" $FilterPrice;
       
$FilterSet TRUE;    }
 
  if (
'FilterSet')
    {
      
$result mysql_query("select * from gpstable
      WHERE id > 0
      
$FilterIdQuery
      
$FilterPicQuery  
      
$FilterMakeQuery
      
$FilterScrSizeQuery
      
$FilterResQuery  
      
$FilterPriceQuery  
      order by price"
);
      print 
$resultarray[0];
    }

    
// Otherwise, use the base query, without conditions from input:
  
else
      {  
$result mysql_query("select * from gpstable 
          order by price"
);
          print 
$resultarray[0]; }

// Print Results
   
$resultarray mysql_fetch_array($result);

// Count resultant rows
  
$num mysql_num_rows($result);
  
  print 
"<br /><br /><br /><br /><br /><br /><br /><br />";
  print 
"Displaying $num records.<br/>";
  print 
"<table width=200 border=1>\n";

// print table columns as headings
  
for ($i 0$i mysql_num_fields($result); $i++)
    {  print 
'<th>' mysql_field_name($result$i) . '</th>';  }

   while(
$getrow mysql_fetch_row($result))
    {
     print 
"<tr>\n";
     foreach (
$getrow as $field
         { print 
"\t<td>$field</td>\n"; }
     print 
"</tr>\n";
   }   
?>
</form></body></html>
hoco is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-04-2009, 01:22 AM Re: Displaying an SQL table from user inputs
metho's Avatar
Ultra Talker

Posts: 481
Location: Gold Coast - Brisbane QLD, Australia
Trades: 0
if ('FilterSet')

should be

if ($FilterSet)

Yes?
__________________
I do
Please login or register to view this content. Registration is FREE
based.
Spend a lot of time in
Please login or register to view this content. Registration is FREE
.
And
Please login or register to view this content. Registration is FREE
chews up the rest.
metho is offline
Reply With Quote
View Public Profile Visit metho's homepage!
 
Old 01-04-2009, 01:37 AM Re: Displaying an SQL table from user inputs
Average Talker

Posts: 21
Name: Hal
Trades: 0
Good Catch metho!!

I fixed $FilterSet but still get entire table.
hoco is offline
Reply With Quote
View Public Profile
 
Old 01-04-2009, 04:39 AM Re: Displaying an SQL table from user inputs
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Add a space before each "AND" in your concatenation part otherwise the AND will butt up to the previous value.


echo out the query before the DB call to show any problems with what query is being sent.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?

Last edited by chrishirst; 01-04-2009 at 04:40 AM..
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 01-04-2009, 03:33 PM Re: Displaying an SQL table from user inputs
Average Talker

Posts: 21
Name: Hal
Trades: 0
I changed all
"AND ....
to
" AND ....
& same thing displays

When I add: echo $result;
It displays: Resource id #13
hoco is offline
Reply With Quote
View Public Profile
 
Old 01-05-2009, 02:38 AM Re: Displaying an SQL table from user inputs
Novice Talker

Posts: 10
Name: Tim
Trades: 0
That shows that you're echoing a resource (which mysql_query() returns).
You want to echo $resultarray or $num.
Or do a echo mysql_result($result, 0); depending on what you want to get from $result.

~Tim

P.S - Isn't MySQL fun? (sarcasm)
Crystalmyst is offline
Reply With Quote
View Public Profile
 
Old 01-05-2009, 08:30 AM Re: Displaying an SQL table from user inputs
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
you need to echo the actual QUERY not the result from it.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Reply     « Reply to Displaying an SQL table from user inputs
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.41561 seconds with 12 queries