need some help with this search script

Discussion in 'PHP' started by dougvcd, Aug 11, 2007.

  1. #1
    cant get this to work not sure if i have done it right or missing some thing
    what should happen you select from the box then the results shown
    but nothing happening

    <form name="search" method="post" action="<?=$PHP_SELF?>"> 
    <Select NAME="location">
    <Option VALUE="devon">Devon</option>
    <Option VALUE="cornwall">Cornwall</option>
    <Option VALUE="wales">Wales</option>
    </Select>
    <input type="hidden" name="searching" value="yes" />
    <input type="submit" name="search" value="Select" />
    </form>
      
    <?php
    
    // Connects to your Database 
    mysql_connect("6", "c", "3") or die(mysql_error()) ; 
    mysql_select_db("users") or die(mysql_error()) ; 
    
    //Retrieves data from MySQL 
    $data = mysql_query("SELECT * FROM members WHERE region='hire' and parklocation='location'") or die(mysql_error()); 
    //Puts it into an array 
    while($info = mysql_fetch_array( $data )) 
    { 
    $email = $info['email'];
    //Outputs the image and other data
    Echo "<img src=http://www.uk-caravan-hire.com/images/".$info['pname'] ." alt=\"Image\" align=\"left\" width=\"150px\" height=\"100px\" hspace=\"10px\" > <br>";
    Echo "<b>Name:</b> ".$info['name'] . " <br>";
    echo "<b>Email : </b><a href=mailto:$email>Click Here.</a><br>";
    Echo "<b>Contact:</b> ".$info['contact'] . " <br>";
    Echo "<b>Park Name:</b> ".$info['parkname'] . " <br>";
    Echo "<b>Park Location:</b> ".$info['parklocation'] . " <br>";
    Echo "<b>Details:</b> ".$info['caravandetails'] . " <br>";
    	if(empty($info[web]))
    {
    $web = "Not Provided" ;  }
    else {
    $web = "<a href=$info[web]>Visit My Website</a> "; }
    
    Echo "<b>Web site :  </b>$web<br>";
    Echo "<b>Availability:</b> ".$info['avail'] . " <hr>";
     
    }
    ?> 
    PHP:
     
    dougvcd, Aug 11, 2007 IP
  2. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    This looks wrong to me...

    
    $data = mysql_query("SELECT * FROM members WHERE region='hire' and parklocation='location'") or die(mysql_error()); 
    
    PHP:
    That will try to select members with a region literally equal to the word 'hire' and a location literally equal to the word 'location'.

    Clearly, you won't have locations called "location" so it will return nothing.

    What it should do is this...
    
    $data = mysql_query("SELECT * FROM members WHERE region='hire' and parklocation='".$_POST['location']."'") or die(mysql_error()); 
    
    PHP:
    which will insert the value from the form into the query.
     
    ecentricNick, Aug 11, 2007 IP
  3. void

    void Peon

    Messages:
    119
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #3
    
    $data = mysql_query("SELECT * FROM members WHERE region='hire' and parklocation='".mysql_real_escape_string($_POST['location'])."'") or die(mysql_error()); 
    
    PHP:
     
    void, Aug 11, 2007 IP
  4. dougvcd

    dougvcd Peon

    Messages:
    267
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    thanks for info it worked sort of
    when the page loads it already has info which should not be there
    and after a query if you run another the first one is still there
    must need more work
    but thanks
    Doug
    if you want to see what i mean check it out
    www.uk-caravan-hire.com then for hire
     
    dougvcd, Aug 11, 2007 IP
  5. void

    void Peon

    Messages:
    119
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Could you post the database table structure & data along with your current script? That'd help me work out what's going on.
     
    void, Aug 12, 2007 IP
  6. dougvcd

    dougvcd Peon

    Messages:
    267
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    have sent private pm
    Doug
     
    dougvcd, Aug 12, 2007 IP
  7. dougvcd

    dougvcd Peon

    Messages:
    267
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    ok here is the code

    <form name="search" method="post" action="<?=$PHP_SELF?>"> 
    <Select NAME="location">
    <option value="*">All</option>
    <Option VALUE="devon">Devon</option>
    <Option VALUE="cornwall">Cornwall</option>
    <Option VALUE="wales">Wales</option>
    <option value="scotland">Scotland</option>
    </Select>
    <input type="hidden" name="searching" value="yes" />
    <input type="submit" name="search" value="Search" />
    </form>
      
    <?php
    
    
    // Connects to your Database 
    mysql_connect("6", "c", "3") or die(mysql_error()) ; 
    mysql_select_db("c") or die(mysql_error()) ; 
    
    //Retrieves data from MySQL 
    $data = mysql_query("SELECT * FROM members WHERE region='hire' or region='both' and parklocation ='".mysql_real_escape_string($_POST['location'])."'") or die(mysql_error());
    //Puts it into an array 
    while($info = mysql_fetch_array( $data )) 
    { 
    $email = $info['email'];
    //Outputs the image and other data
    Echo "<img src=http://www.uk-caravan-hire.com/images/".$info['pname'] ." alt=\"Image\" align=\"left\" width=\"150px\" height=\"100px\" hspace=\"10px\" > <br>";
    Echo "<b>Name:</b> ".$info['name'] . " <br>";
    echo "<b>Email : </b><a href=mailto:$email>Click Here.</a><br>";
    Echo "<b>Contact:</b> ".$info['contact'] . " <br>";
    Echo "<b>Park Name:</b> ".$info['parkname'] . " <br>";
    Echo "<b>Park Location:</b> ".$info['parklocation'] . " <br>";
    Echo "<b>Details:</b> ".$info['caravandetails'] . " <br>";
    	if(empty($info[web]))
    {
    $web = "Not Provided" ;  }
    else {
    $web = "<a href=$info[web]>Visit My Website</a> "; }
    
    Echo "<b>Web site :  </b>$web<br>";
    Echo "<b>Availability:</b> ".$info['avail'] . " <hr>";
     
    } 
    
    ?>  
    PHP:
    file to big to attach
     
    dougvcd, Aug 12, 2007 IP
  8. void

    void Peon

    Messages:
    119
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Your updated SQL statement has a problem - adding the OR has changed how it works. Change it to:
    
    $data = mysql_query("SELECT * FROM members WHERE (region='hire' or region='both') and parklocation ='".mysql_real_escape_string($_POST['location'])."'") or die(mysql_error());
    
    Code (markup):
     
    void, Aug 12, 2007 IP
  9. dougvcd

    dougvcd Peon

    Messages:
    267
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #9
    thanks buddy that works ok now
    can you tell me how to make it show all if required i put *
    cheers
    Doug
     
    dougvcd, Aug 12, 2007 IP
  10. void

    void Peon

    Messages:
    119
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #10
    You just need an if statement to change the SQL query, this should do the trick:
    //Retrieves data from MySQL
    $strLocation = mysql_real_escape_string($_POST['location']);
    if ($strLocation == "*")
    {
      $data = mysql_query("SELECT * FROM members WHERE region='hire' OR region='both'") or die(mysql_error());
    }
    else
    {
      $data = mysql_query("SELECT * FROM members WHERE (region='hire' OR region='both') AND parklocation='$strLocation'") or die(mysql_error());
    }
    
    PHP:
    Another slightly shorter variation:
    //Retrieves data from MySQL
    $strLocation = mysql_real_escape_string($_POST['location']);
    $strQuery = "SELECT * FROM members WHERE (region='hire' OR region='both')";
    if ($strLocation != "*")
    {
      $strQuery .= " AND parklocation='$strLocation'";
    }
    $data = mysql_query($strQuery) or die(mysql_error());
    
    PHP:
    Sorry, had a typo in 2nd PHP snippet.
     
    void, Aug 12, 2007 IP
  11. dougvcd

    dougvcd Peon

    Messages:
    267
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #11
    very big thanks
    all working ok
    does what it says on the tin hehehe
    Doug
     
    dougvcd, Aug 12, 2007 IP
  12. void

    void Peon

    Messages:
    119
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #12
    No prob :)
     
    void, Aug 12, 2007 IP