Php: passing variable to new page, but database returns first record

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • isak
    New Member
    • Mar 2010
    • 2

    Php: passing variable to new page, but database returns first record

    I have a webpage that lists the results of a preliminary database search -- (http://www.txlivingus.com/ourListings.php, then click on"non-mls listings." Each item on the list links to a detail page -- the variable is passed to the next page via "get". The correct record id appears when you roll over the link, but when you click the link, the new page seems to only pull the first record from the database and not the one I am actually selecting. No matter which record you select, you get the same info which happens to be the first record in the database.

    Help??
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    Can you show us the code that is pulling the record from the database?

    I would bet that the variable you are using for the WHERE clause of your query is somehow being populated as TRUE, rather than the value you are passing via GET.

    For example, one of my past mistakes looked something like this:
    [code=php]<?php
    $id = (int)isset($_GE T['id']);
    $sql = "SELECT stuff FROM tbl WHERE `id` = {$id}";
    mysql_query($sq l);
    ?>[/code]
    In there, the return value of the isset() function is being used as the ID, rather than the value passed via the $_GET protocol. - And because TRUE get's converted to 1, the first record of the table would always be pulled out, regardless of what I passed.

    The correct version of that should of course be:
    [code=php]<?php
    if(isset($_GET['id'])) {
    $id = (int)$_GET['id'];
    $sql = "SELECT stuff FROM tbl WHERE `id` = {$id}";
    mysql_query($sq l);
    }
    else {
    echo "No ID passed!";
    }
    ?>[/code]

    Perhaps you are having a similar problem?

    Comment

    • isak
      New Member
      • Mar 2010
      • 2

      #3
      Here's the code that pulls the info from the database. The bolded text is the link that passes the variable to the detail page:

      Code:
       <?php do { ?>
           <tr>
             <td width="162" valign="top"><div align="center">[B]<a href="propertyDetail.php?id=<?php echo $row_rsForm4['id']; ?>">[/B]<img src="http://www.txlivingus.com/listings/data/form_4/files/<?php echo $row_rsForm4['element_78']; ?>" alt="Texas Living Real Estate in Centerville | Centerville, Texas" border="0" /></a><a href="propertyDetail.php?id=<?php echo $row_rsForm4['id']; ?>" border="0"></a></div></td>
             <td width="426"><h4><a href="propertyDetail.php?id=<?php echo $row_rsForm4['id']; ?>"><?php echo $row_rsForm4['element_73']; ?> <?php echo $row_rsForm4['element_72']; ?></a> |<a href="<?php echo $row_rsForm4['element_156']; ?>"><?php echo $row_rsForm4['element_74']; ?></a><br />
               $<?php echo number_format($row_rsForm4['element_35'],0,'.',','); ?> / <?php echo $row_rsForm4['element_114']; ?></h4>
                 <br />
                <p><?php echo $row_rsForm4['element_53']; ?><br />
               <span class="boldText"><?php echo $row_rsForm4['element_1']; ?> acres</span></p></td>
           </tr>
           <?php } while ($row_rsForm4 = mysql_fetch_assoc($rsForm4)); ?>
      Here's the code from the head of the page:
      Code:
      <?php
      if (!function_exists("GetSQLValueString")) {
      function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
      {
        if (PHP_VERSION < 6) {
          $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
        }
      
        $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
      
        switch ($theType) {
          case "text":
            $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
            break;    
          case "long":
          case "int":
            $theValue = ($theValue != "") ? intval($theValue) : "NULL";
            break;
          case "double":
            $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
            break;
          case "date":
            $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
            break;
          case "defined":
            $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
            break;
        }
        return $theValue;
      }
      }
      
      mysql_select_db($database_connTxLiving, $connTxLiving);
      $query_rsForm4 = "SELECT * FROM ap_form_4 WHERE ap_form_4.element_114 like 'Active' ORDER BY element_35 DESC";
      $rsForm4 = mysql_query($query_rsForm4, $connTxLiving) or die(mysql_error());
      $row_rsForm4 = mysql_fetch_assoc($rsForm4);
      $totalRows_rsForm4 = mysql_num_rows($rsForm4);
      ?>
      The coding is done via Dreamweaver CS4.

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Ok.

        I don't see you actually fetching and using the ID in the second page. It only uses a static SQL query. - It needs to be something like my second example above; the ID needs to be retrieved and placed in the SQL query.

        Also, in your first code, why do you use a do-while loop? Unless there is a copy of the while condition above the loop, the first loop should be outputting an invalid row. - We typically use a simple while loop to iterate through SQL result sets.

        Comment

        Working...