troubleshooting Question

Passing variable from URL to php mysql script not working

Avatar of mnoel76
mnoel76 asked on
PHP
5 Comments1 Solution1222 ViewsLast Modified:
I will do my best to explain this issue clearly.  
I am trying to pass a variable by hard coding it into the url with hopes that my DB query will display the results properly.  Currently my dynamically generated pages do not have any variables in the url.  Can I pass a variable to a mysql query through the URL?  What I am trying to do is allow a user to click a link with that variable being passed in the url.  This results page would have had the search variables passed from the previous page.  The url would usually look like this.  http://www.milehighlistings.com/rental_properties.php.  The url I am trying to get wokring would look like this ttp://www.milehighlistings.com/rental_properties.php?countyID=Denver.
Here is the code for the page that I am trying to pass the variable to.

<? session_start(); ?>
<div id="loading"> Performing Search... </div>
<?

      ### DEBUG
      $debugP = 0;
      ### END DEBUG

      #Fetch database connection
      require_once('Connections/myconn.php');
      mysql_select_db($database_myconn,$myconn);
      require_once('myfunctions.php');
      $mysql = new mysql();
      $mysql->conn = $myconn;
      
      #Fetch Header
      include('header_rentals.php');
      
      #### BUILD SEARCH SQL BASED ON SEARCH TYPE ####
      
      #defauts
      
      $maxRows_p = 10;
      $pageNum_p = 0;
      if (isset($_GET['pageNum_p'])) {
        $pageNum_p = $_GET['pageNum_p'];
      }
      $startRow_p = $pageNum_p * $maxRows_p;
      $limit = ' LIMIT '.$startRow_p.', '.$maxRows_p;
      
                  
      
         //Add state search
         if(!empty($_REQUEST['state']))  {
               $search[] = 'state = "'.$_REQUEST['state'].'"';
          //Add zip code search
          if(!empty($_REQUEST['zip']))
             $search[] = 'zip = '.$_REQUEST['zip'];
         //Add Price From
         if(!empty($_REQUEST['pfrom']) && ($_REQUEST['pfrom'] != '-1'))
             $search[] = 'rent >= '.$_REQUEST['pfrom'];
         //Add Max Price
         if(!empty($_REQUEST['pto']) && ($_REQUEST['pto'] != '-1'))
               $search[] = 'rent <= '.$_REQUEST['pto'];          
         //Add Property ID
           if(!empty($_REQUEST['id']))
               $search[] = 'id = '.$_REQUEST['id'];              
          //Add Property Type
           if(!empty($_REQUEST['cid']))
               $search[] = 'cid = '.$_REQUEST['cid'];
         //Add Min Bed
           if(!empty($_REQUEST['bed']))
               $search[] = 'bed >= "'.$_REQUEST['bed'].'"';
          //Add Min Bath
           if(!empty($_REQUEST['bath']))
                $search[] = 'bath >= "'.$_REQUEST['bath'].'"';
          //Add city
           if(!empty($_REQUEST['city']))
               $search[] = 'city = "'.$_REQUEST['city'].'"';
         //Add min square feet
           if(!empty($_REQUEST['sqft']))
               $search[] = 'sqft >= '.$_REQUEST['sqft'];
          //Add Garage
           if(!empty($_REQUEST['garage']))
               $search[] = 'garage = "'.$_REQUEST['garage'].'"';
          //Add county ID
           if(!empty($_REQUEST['countyID']))
               $search[] = 'countyID = '.$_REQUEST['countyID'].' ';
           //Add pets
           if(!empty($_REQUEST['pets']))
               $search[] = 'pets = "'.$_REQUEST['pets'].'"';
           //Add Subdivision         
           if(!empty($_REQUEST['subdiv']))
               $search[] = 'subdiv = "'.$_REQUEST['subdiv'].'"';

          //implode to search string on ' and ';
          $searchStr = @implode(' and ',$search);
           $str = "";
               
         if(!empty($_REQUEST['keyword'])) {
           $str = ' and MATCH(description) AGAINST ("'.$_REQUEST['keyword'].'" in BOOLEAN MODE)';
}          $searchStr .= $str;
           $sql = 'select * FROM rentals WHERE (expires > NOW()) and active = "Yes" and leased = "No" and ';
           $sql .= $searchStr;
               print $sql;
      
                //Add column sorting
            if($_REQUEST['sort'] != '')
                  $sort = ' order by rent asc ';
            else
                  $sort = $_REQUEST['sort'];
            
            ### DEBUG
            if($debugP) echo 'Advanced Search Sql<hr>'.$sql;
            
            $error['Results'] = 'No results found, please search again';
            
}      

      ### Finished Building search sql and execting #####
      $sql .= $sort . $limit;
                           
      //Perform search
      $searchResults = $mysql->exSql($sql);
      

      ### BUILD OUTPUT ####
      
      if (isset($_GET['totalRows_p'])) {
        $totalRows_p = $_GET['totalRows_p'];
      } else {
        $all_p = mysql_query($sql.$sort);
        $totalRows_p = @mysql_num_rows($all_p);
      }
      $totalPages_p = ceil($totalRows_p/$maxRows_p)-1;
      


?>
<div align="left" class="locText"><a href="index.php" class="locLink">Home</a> <span class="locArrow">&nbsp;>&nbsp;</span> <a href="rent.php" class="locLink">Search Rental Listings</a> <span class="locArrow">&nbsp;>&nbsp;</span>Properties For Rent</div>
<hr size="1" color="#666666">
<table border="0" align="center">
  <tr>
    <td align="center">
      <?php if ($pageNum_p > 0) { // Show if not first page ?>
      <a href="<?php printf("%s?pageNum_p=%d%s", $currentPage, 0, $queryString_p); ?>" class="pageLink">First</a> |
      <?php } // Show if not first page ?>
      <?php if ($pageNum_p > 0) { // Show if not first page ?>
      <a href="<?php printf("%s?pageNum_p=%d%s", $currentPage, max(0, $pageNum_p - 1), $queryString_p); ?>" class="pageLink">Previous</a> |
      <?php } // Show if not first page ?>
      <?php if ($pageNum_p < $totalPages_p) { // Show if not last page ?>
      <a href="<?php printf("%s?pageNum_p=%d%s", $currentPage, min($totalPages_p, $pageNum_p + 1), $queryString_p); ?>" class="pageLink">Next</a> |
      <?php } // Show if not last page ?>
      <?php if ($pageNum_p < $totalPages_p) { // Show if not last page ?>
      <a href="<?php printf("%s?pageNum_p=%d%s", $currentPage, $totalPages_p, $queryString_p); ?>" class="pageLink">Last</a>
      <?php } // Show if not last page ?>
    </td>
  </tr>
</table>

<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td class="pageText" >Showing: <strong><?php echo ($startRow_p + 1) ?> - <?php echo min($startRow_p + $maxRows_p, $totalRows_p) ?> of <?php echo $totalRows_p ?></strong> Rental Listings in Colorado</td>
          <td align="right" class="pageText"></td>
        </tr>
      </table></td>
  </tr>
  <tr>
    <td height="5"><img src="images/pixel.gif" width="1" height="1" alt=""></td>
  </tr>
  <tr>
    <td><table width="100%" border="0" cellspacing="1" cellpadding="4" class="resBorder">
        <tr>
          <td class="colText">Address</td>
              <?
                    if(isset($_REQUEST['fromZip']) && ($_REQUEST['fromZip'] != ''))
                        print '<td class="colText">Miles</td>';
            ?>
          <td class="colText">City</td>
          <td class="colText">ST</td>
          <td class="colText">Rent</td>
          <td class="colText">Beds</td>
          <td class="colText">Baths</td>
          <td class="colText">Sqft</td>
        </tr>
      <? while($row_p = @mysql_fetch_assoc($searchResults)) { ?>
        <tr valign="top">
          <td class="rowText"><a href="rental_detail.php?id=<? echo $row_p['id']; ?>" class="rowLinkBold" ><? echo $row_p['address']; ?></a></td>
                <?
                    if(isset($_REQUEST['fromZip']) && ($_REQUEST['fromZip'] != ''))
                        print '<td class="rowText">'.number_format($row_p['Miles'],2,'.',',').'</td>';
            ?>
          <td class="rowText"><? echo $row_p['city']; ?></td>
          <td class="rowText"><? echo $row_p['state']; ?></td>
          <td class="rowText"><? echo Money($row_p['rent'],1); ?></td>
          <td class="rowText"><? echo $row_p['bed']; ?></td>
          <td class="rowText"><? echo $row_p['bath']; ?></td>
          <td class="rowText"><? echo $row_p['sqft']; ?></td>
        </tr>
        <tr valign="top">
          <td class="descText" colspan="8"><table border="0" cellspacing="0" cellpadding="4">
              <tr valign="top">
                <? //fetch photo from database
      $sql = "select * from rental_photos where ptid = ". $row_p['id']." and porder = '1' ";
      //echo $sql; //debug sql output
      $pRS = mysql_query($sql,$myconn) or die(mysql_error());
      //fetch assoc array
      $row_photo = mysql_fetch_assoc($pRS);
      // if image exist
      if ( mysql_num_rows($pRS) > 0 )
            $imageSRC = $row_photo['location'];
      else //no photo
            $imageSRC = "noimage.jpg";
      ?>
                <td class="descText"><a href="rentaldetail.php?id=<? echo $row_p['id']; ?>"><img src="admin/photos/uploads/small_thumbs/tn_<? echo $imageSRC; ?>" border="0" alt="<? echo $row_photo['caption']; ?>"></a></td>
                <td class="descText"><? echo substr($row_p['description'],0,150); ?>...<a href="rentaldetail.php?id=<? echo $row_p['id']; ?>" class="rowLink">more..</a><a class="rowLink" href="saveListing.php?id=<? echo $row_p['id']; ?>" title="Click to save listing"><br>
                  save listing</a></td>
              </tr>
            </table></td>
        </tr>
      <? } ?>
      </table></td>
  </tr>


  <tr>
    <td height="5"><img src="images/pixel.gif" width="1" height="1" alt=""></td>
  </tr>
  <tr>
    <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td class="pageText">&nbsp;</td>
          <td align="right"></td>
        </tr>
      </table></td>
  </tr>
</table>
<table border="0" align="center">
  <tr>
    <td align="center"><?php if ($pageNum_p > 0) { // Show if not first page ?>
        <a href="<?php printf("%s?pageNum_p=%d%s", $currentPage, 0, $queryString_p); ?>" class="pageLink">First</a> |
        <?php } // Show if not first page ?>
        <?php if ($pageNum_p > 0) { // Show if not first page ?>
        <a href="<?php printf("%s?pageNum_p=%d%s", $currentPage, max(0, $pageNum_p - 1), $queryString_p); ?>" class="pageLink">Previous</a> |
        <?php } // Show if not first page ?>
        <?php if ($pageNum_p < $totalPages_p) { // Show if not last page ?>
        <a href="<?php printf("%s?pageNum_p=%d%s", $currentPage, min($totalPages_p, $pageNum_p + 1), $queryString_p); ?>" class="pageLink">Next</a> |
        <?php } // Show if not last page ?>
        <?php if ($pageNum_p < $totalPages_p) { // Show if not last page ?>
        <a href="<?php printf("%s?pageNum_p=%d%s", $currentPage, $totalPages_p, $queryString_p); ?>" class="pageLink">Last</a>
        <?php } // Show if not last page ?>
    </td>
  </tr>
</table>
<p>&nbsp;</p>
<?
      ## if no results where found
      if(@mysql_num_rows($searchResults)<=0){
               $error['Results'] = 'No results found, please search again';
            foreach($error as $name => $value)
                  print '<div align=center class="error">'.$name . ': ' . $value.'</div>';
      }
      ##Fetch Footer
      include('footer2.php');
?>
<script>
document.getElementById('loading').style.display = 'none';
</script>
ASKER CERTIFIED SOLUTION
MasonWolf

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros