SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Dec 2004
    Location
    canada
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Carrying over the ID onto the next page...

    Hi gang
    Many of you helped me create this script to list products I have stored in a table:

    Code:
    // create query 
    $query = "SELECT id, name, shortdesc, bpix, pixalt FROM product"; 
    
    // execute query 
    $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 
    $numrows = 0;  
            print "<table><tr>";  
        while($row = mysql_fetch_assoc($result)){  
        echo "<td align=\"middle\">"; 
        echo "<a href='product.php?id=$row[id]'>$row[name]</a><br>";    
            echo "<img src=images/$row[bpix] alt=$row[pixalt]>";  
            $numrows++;  
            if($numrows > 2) {  
            echo "</td></tr><tr>";  
            $numrows = 0;  
                } else {  
             echo "</td>";  
                }  
            }
    This code basically looks into the DB and pulls out any records... As you can see in the line highlighted in red above, the record comes out with a link to "product.php" which right now links perfectly to the product.php page but without the information on that particular product... For some reason it only pulls out the product info for the first record I have in the DB... here's what product.php looks like:

    Code:
    // create query 
    $query = "SELECT name, fulldesc FROM product"; 
    
    // execute query 
    $result = mysql_query($query) or die ("Error in query: ".$query."<br/>".mysql_error());  
        $row = mysql_fetch_array($result);
            echo "<table cellpadding=1 border=1 width=100%>"; 
    		echo "<tr>";
    		echo "<td>$row[name]</td>";
    		echo "</tr>"; 
    		echo "<tr>";
    		echo "<td>$row[fulldesc]</td>";
    		echo "</tr>"; 
    		echo "</table>";
    Thanks for your help!

  2. #2
    SitePoint Zealot agoossens's Avatar
    Join Date
    Mar 2004
    Location
    Adelaide, Australia
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you want product.php to display the chosen product, you'll need to craft your SQL statement so that it selects that product. Currently it selects all products, and the single call to mysql_fetch_array() returns the first one in the result set.

    You pass the product ID in $_GET['id'] - craft the SQL statement from that.
    This space for rent.

  3. #3
    SitePoint Addict
    Join Date
    Dec 2004
    Location
    canada
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do I only put the $_GET['id'] on the product.php page or on the page where the link is as well? Do you have a sample script that I can take a look at?

    Thanks

  4. #4
    SitePoint Addict
    Join Date
    Dec 2004
    Location
    canada
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    did some looking around... it was actually quite simple to do... Oh! PHP, how I quickly am falling for you...

    This is how the code looks like now. See the red lines

    Code:
    $id=$_GET['id'];
    ...then some more stuff here
    
    // create query 
    $query = "SELECT name, fulldesc FROM product WHERE id='$id'"; 
    
    // execute query 
    $result = mysql_query($query) or die ("Error in query: ".$query."<br/>".mysql_error());  
        $row = mysql_fetch_array($result);
            echo "<table cellpadding=1 border=1 width=100%>"; 
    		echo "<tr>";
    		echo "<td>$row[name]</td>";
    		echo "</tr>"; 
    		echo "<tr>";
    		echo "<td>$row[fulldesc]</td>";
    		echo "</tr>"; 
    		echo "</table>";


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •