PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 21st, 2012, 03:29 PM
ccmweb ccmweb is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 4 ccmweb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 44 m 15 sec
Reputation Power: 0
Error with search query from form

I have a DB of homes for sale. It has columns for Zip, Bedrooms, Baths, and State etc. I also have a form where users enter this info, some of which is optional. My form is below:

Code:
<form id="form1" name="form1" method="post" action="inner2.php">  <table width="180" border="0" cellpadding="5">
  <tr>
    <td>

    <input name="city" type="text" class="textfield" value="City" onclick="this.value='';" onfocus="this.select()" onblur="this.value=!this.value?'City':this.value;"/>
    </td>
    <td>  <select name="state"  class="dropdown" >
      <option value="State">State</option>
  <option value="CA">CA</option>
  <option value="OK">OK</option>
    <option value="OR">OR</option>   
      </select>
      </td>
  </tr>
  <tr>
    <td>
      <select name="distance"  class="dropdown">
      <option>Within 10 miles</option>
  <option>Within 30 miles</option>
  <option>Within 50 miles</option>
  <option>Within 100 miles</option>
    <option>Within 250 miles</option>   
      </select>
      
     </td>
    <td> <input name="zip" type="text" class="textfield" onclick="this.value='';" onfocus="this.select()" onblur="this.value=!this.value?'Zip':this.value;" value="Zip"/></td>
  </tr>
</table>

        


            <table width="180" border="0" cellpadding="5" style="margin-top:70px;">
               <tr>
                <td>Beds</td>
           <td>Baths</td>
              </tr>
              <tr>
                <td><select name="bedrooms"  class="dropdown">
                  <option value="any">Any</option>
                  <option value="1">1</option>
                  <option value="2">2</option>
                  <option value="3">3</option>
                  <option value="4">4</option>
                </select></td>
                <td><select name="baths"  class="dropdown">
                  <option value="any">Any</option>
                  <option value="1">1</option>
                  <option value="2">2</option>
                  <option value="3">3</option>
                  <option value="4">4</option>
                </select></td>
              </tr>
               <tr>
                <td>Min sq ft.</td>
           <td>Property Type</td>
              </tr>
              <tr>
                <td><select name="sq_ft1"  class="dropdown">
                  <option>Any</option>
                  <option>500 sq ft</option>
                  <option>800 sq ft</option>
                  <option>1000 sq ft</option>
                  <option>1200 sq ft</option>
                </select></td>
                <td><select name="property_type"  class="dropdown">    
                     <option>Select One</option>
                  <option>Single Wide</option>
                  <option>Double Wide</option>
                  <option>Triple Wide</option>
                  <option>Your Momma Wide</option>
                </select></td>
              </tr>
            </table>

          



            <table width="180" border="0" cellpadding="5" style="margin-top:60px;">
               <tr>
                <td>Price Range</td>
           <td>&nbsp;</td>
              </tr>
              <tr>
                <td><select name="price_range_min"  class="dropdown" >
                <option>Min</option>
                  <option>$1000</option>
                  <option>$5000</option>
                  <option>$10000</option>
                  <option>$15000</option>
                  <option>$20000</option>
                  <option>$30000</option>
                  <option>$40000</option>
                </select></td>
                <td><select name="price_range_max"  class="dropdown" >
                <option>Max</option>
                  <option>$5000</option>
                  <option>$10000</option>
                  <option>$15000</option>
                  <option>$20000</option>
                  <option>$30000</option>
                  <option>$60000</option>
                  <option>$80000</option>
                </select></td>
              </tr>
               <tr>
                <td>Min sq ft.</td>
           <td>&nbsp;</td>
              </tr>
              <tr>
                <td><select name="sq_ft2_min"  class="dropdown">
                  <option>Any</option>
                  <option>Within 30 miles</option>
                  <option>Within 50 miles</option>
                  <option>Within 100 miles</option>
                  <option>Within 250 miles</option>
                </select></td>
                <td><select name="sq_ft2_max"  class="dropdown">
                  <option>Max</option>
                  <option>Single Wide</option>
                  <option>Double Wide</option>
                  <option>Triple Wide</option>
                  <option>Your Momma Wide</option>
                </select></td>
              </tr>
              
            </table>

            
            
            <table width="261" border="0" cellpadding="5" style="margin-top:40px;">
              <tr>
                <td width="79">Enter ID #</td>
                <td width="156"> <input name="home_id" type="text" class="textfield_id" onclick="this.value='';" onfocus="this.select()" onblur="this.value=!this.value?'':this.value;" value=""/></td>
              </tr>
   
              </table>
              
              <table width="100%" border="0" cellpadding="5">
  <tr>
    <td width="68%">&nbsp;</td>
    <td width="32%">&nbsp;</td>
  </tr>
  <tr>
    <td><p align="right" style=" padding-bottom:15px; "><input type="reset" value="clear" style="text-decoration:underline; background-color:#000; color:#fff; font-weight:bold; border:none;" border="0"  ></td>
    <td><p align="right"><INPUT TYPE="image" SRC="images/search.png" 
              BORDER="0" ALT="SUBMIT!"></p> </td>
  </tr>
</table>

            
            
            
            
                 </form>
   
      



Here is the PHP to handle the process.


PHP Code:
 $city=$_POST['city'];
$state=$_POST['state'];
$bedrooms=$_POST['bedrooms'];
$baths=$_POST['baths'];
$zip=$_POST['zip'];

$con mysql_connect("mysql.testing.com","testing","testing");

 
mysql_select_db("testing"$con);
$query "SELECT * FROM test WHERE 1=1 ";
if(
$state != "State"$query .= "AND State = " .$state" ";
if(
$bedrooms != "any"$query .= "AND Bedrooms = " .$bedrooms" ";
if(
$baths != "any"$query .= "AND Baths = " .$baths" ";
if(
$zip != "Zip"$query .= "AND Zip = " .$zip" ";

$query .= ' ';
echo 
$query;
$result mysql_query($query);

while(
$row mysql_fetch_array($result) or die(mysql_error()))
  {    ........ 


You'll notice I'm using a string builder. Everything works perfectly for bedrooms, baths, and zip, but when I select a state, I get this error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/inner2.php on line 251
Unknown column 'CA' in 'where clause'

The resulting echo for $query when I select California is "SELECT * FROM test WHERE 1=1 AND State = CA "

I get the same error when I try to code for "City".

I've been stuck on this all week, so any help would be much appreciated. I don't understand how I got it to work so easily for bedrooms, baths, and zip, but not State. This is my first post, so please let me know if I did it wrong. Thanks!

Reply With Quote
  #2  
Old November 21st, 2012, 03:35 PM
gw1500se gw1500se is online now
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,907 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 1 Month 12 h 5 m 45 sec
Reputation Power: 581
Echo the resulting string '$query' to make sure it is what you expect. We can't be much help without seeing the resulting string.

P.S. I suggest you consider rewriting your database code to use PDO rather than the depreciated MySQL extensions.
Comments on this post
ccmweb agrees!
__________________
There are 10 kinds of people in the world. Those that understand binary and those that don't.

Reply With Quote
  #3  
Old November 21st, 2012, 03:40 PM
ccmweb ccmweb is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 4 ccmweb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 44 m 15 sec
Reputation Power: 0
Quote:
Originally Posted by gw1500se
Echo the resulting string '$query' to make sure it is what you expect. We can't be much help without seeing the resulting string.

P.S. I suggest you consider rewriting your database code to use PDO rather than the depreciated MySQL extensions.


The resulting echo for $query when I select California is ' SELECT * FROM test WHERE 1=1 AND State = CA '

I already have a column named 'State', and there is definitely a few homes listed as 'CA'.

Reply With Quote
  #4  
Old November 21st, 2012, 04:18 PM
ccmweb ccmweb is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 4 ccmweb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 44 m 15 sec
Reputation Power: 0
Quote:
Originally Posted by gw1500se
Echo the resulting string '$query' to make sure it is what you expect. We can't be much help without seeing the resulting string.

P.S. I suggest you consider rewriting your database code to use PDO rather than the depreciated MySQL extensions.


I figured it out! I just forgot ' ' marks around the state.

Reply With Quote
  #5  
Old November 21st, 2012, 06:24 PM
msteudel's Avatar
msteudel msteudel is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2002
Location: Seattle, U.S.A.
Posts: 712 msteudel User rank is Lance Corporal (50 - 100 Reputation Level)msteudel User rank is Lance Corporal (50 - 100 Reputation Level)msteudel User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 4 Days 11 h 4 m 59 sec
Reputation Power: 11
Quote:
Originally Posted by ccmweb
The resulting echo for $query when I select California is ' SELECT * FROM test WHERE 1=1 AND State = CA '

I already have a column named 'State', and there is definitely a few homes listed as 'CA'.


It's because you don't have quotes around CA.

Reply With Quote
  #6  
Old November 21st, 2012, 06:26 PM
ccmweb ccmweb is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 4 ccmweb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 44 m 15 sec
Reputation Power: 0
You missed the post above you, but thank you anyways. I can't believe those little marks ruined my week.

Reply With Quote
  #7  
Old November 21st, 2012, 07:39 PM
msteudel's Avatar
msteudel msteudel is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2002
Location: Seattle, U.S.A.
Posts: 712 msteudel User rank is Lance Corporal (50 - 100 Reputation Level)msteudel User rank is Lance Corporal (50 - 100 Reputation Level)msteudel User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 4 Days 11 h 4 m 59 sec
Reputation Power: 11
Oops sorry. Glad you got t fixed!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Error with search query from form

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap