从URL获取MySQL ID以在页面上显示信息

I have a products page where 8 product images are in a list which is being populated by images stored in a MySQL database. The images all have associated ID's in which a price, product name, and description is also associated with the same ID.

The idea behind what I am trying to do is; When a user clicks on one of the 8 product images, they will be redirected to a "checkout" page which will display the same image, plus all the information that is also stored under that ID in the database.

As of right now, I have the checkout page URL including the ID of the image (url.com/checkout.php?id=1) and I was hoping to find a way to get all the information stored under that ID in the URL to be displayed where called on the page.

Here is my php code that displays the images in the list on the products page:

    // Grab the data from our template table 
            $sql = "select * from templates"; 
            $result = mysql_query($sql) or die ("Could not access DB: " . mysql_error()); 
            while ($row = mysql_fetch_assoc($result)) 
            { 
                echo "<li>"; 
                echo "<a class=\"caption\" href=\"purchase.php?id=\"" . $row['id'] . ">"; 
                // Note that we are building our src string using the ID from the database 
                echo "<img src=\"http://URL-REMOVED.com/file_display.php?id=" . $row['id'] . "\" />"; 
                echo "<span>"; 
                echo "<big>" . $row['name'] . "</big>"; 
                echo $row['description']; 
                echo "</span>"; 
                echo "</a>"; 
                echo "</li>"; 

            }  

Here is the php code that is supposed to gather the information of the clicked product (but doesn't):

    if (isset($_GET['id'])) 
        $id=$_GET['id']; 
    else 
         $id=1; 

    if (isset($_GET['action'])) 
        $action=$_GET['action']; 
     else 
        $action="empty"; 


    switch($action){ 

        case "add": 

        if($_SESSION['cart'][$id]) 
           $_SESSION['cart'][$id]++; 
        else 
            $_SESSION['cart'][$id]=1; 

         break;     

        case "remove": 

        if($_SESSION['cart'][$id]) 
       { 
           $_SESSION['cart'][$id]--; 

           if($_SESSION['cart'][$id]==0) 
    unset($_SESSION['cart'][$id]); 
           } 

          break;     

          case "empty": 
         unset($_SESSION['cart']); 

         break;     
         } 

 //Display Cart 

      if(isset($_SESSION['cart'])) { 

            $total=0; 
            foreach($_SESSION['cart'] as $id => $x) { 
            $result=mysql_query("select image,name,price,description from templates WHERE id=$id"); 
            $myrow=mysql_fetch_array($result); 
            $image=$myrow['image']; 
            $name=$myrow['name']; 
            $price=$myrow['price']; 
            $description=$myrow['description']; 

      }  
    }

And here is the actual HTML/PHP where the information is supposed to be displayed:

  <a class="caption" href="checkout.php"> 
            <img src="http://URL-REMOVED.com/file_display.php?id=<?php $myrow['id'] ?>"/> 
            <span> 
                <big> 
                    <strong><?php $myrow['name'] ?></strong> 
                </big> 

                <div class="price"><?php $myrow['price'] ?></div> 
            </span> 
        </a> 
    </div> 
    <div id="info_form_container"> 
    <div class="product_info"> 
        <div class="control-group"> 
            <strong>Template Name:</strong>  
            <?php $myrow['name'] ?> 
        </div> 

        <div class="control-group"> 
            <strong>Template Description:</strong> 
            <?php $myrow['description'] ?> 
        </div> 

        <div class="control-group"> 
            <strong>Template Price: </strong> 
            <?php $myrow['price'] ?> 
        </div> 
    </div>

I guess I'm not really sure if this is even the best method to take? But I definitely want to have the images stored in the database and I definitely want to call them using the ID...

How can I achieve this? Where am I wrong in my code?

查看全部
dongxingchang9345
dongxingchang9345
2012/08/23 17:47
  • php
  • mysql
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

1个回复