2012-08-23 48 views
1

我有一個產品頁面,其中8個產品圖像位於正在由存儲在MySQL數據庫中的圖像填充的列表中。這些圖像都具有關聯的ID,其中價格,產品名稱和說明也與相同的ID相關聯。從URL獲取MySQL ID以顯示頁面上的信息

我想要做的背後的想法是;當用戶點擊8個產品圖像中的一個時,它們將被重定向到「結帳」頁面,該頁面將顯示相同的圖像,以及也存儲在數據庫中該ID下的所有信息。

截至目前,我的結帳頁面URL包含圖像的ID(url.com/checkout.php?id=1),我希望能找到一種方法來獲取所有存儲在該信息下的信息要在頁面上調用的URL中顯示ID。

這裏是我的PHP代碼,在產品頁面上的列表顯示圖像:

// 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>"; 

      } 

這是應該收集點擊的產品信息的PHP代碼(但沒有) :

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']; 

     } 
    } 

這裏是實際的HTML/PHP,其中的信息是應該顯示:

<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> 

我想我不確定這是否是最好的方法?但我絕對想要將圖像存儲在數據庫中,並且我一定想用ID號給他們打電話...

我該如何做到這一點?我的代碼在哪裏錯了?

+0

檢查表,表中可能沒有任何東西 –

+2

新的應用程序**不應該使用**'mysql_query',而應該使用新的'mysqli'或PDO接口之一。 [SQL注入漏洞](http://bobby-tables.com/)會導致非常嚴重的問題。在你創造一個巨大的混亂之前,你最好切換到其中的一個,你必須清理。 – tadman

+0

好的,謝謝你指出。但是這並不能回答我的問題......而且我的數據庫中的表都已滿。 –

回答

0

編輯:我不認爲我第一次理解你的問題。嘗試使用它來生成您的產品列表,更新連接信息。如果一切正常,請使用以下方法來淨化你的變量和其他存儲連接信息

<?php 
    $mysqli_connection = new mysqli("localhost", "username", "password", "database"); 
    $sql = "SELECT * FROM templates"; 
    $result = $mysqli_connection->query($sql); 
    while ($row = $result->fetch_array(MYSQLI_ASSOC)) { 

     $id = $row['id']; 
     $name = $row['name']; 
     $description = $row['description']; 

     echo '<li>'; 
     echo '<a href="purchase.php?id='.$id.'" class="caption">'; 
     echo '<img src="http://URL-REMOVED.com/file_display.php?id='.$id.'" />'; 
     echo '<span>'; 
     echo '<big>'.$name.'</big>'; 
     echo $description; 
     echo '</span>'; 
     echo '</a>'; 
     echo '</li>'; 
    } 
?> 

OG答:

我覺得你在做什麼就好了。據該方法我用(雖然我看起來有點整潔)。運行你的PHP函數,使用你得到的ID查詢你的MySQL數據庫,並開始轉儲你的信息到你的網站。爲了幫助提高可讀性並減少拼寫混淆,可以將$myrow['whatever']結果指定爲變量來回顯,但這對我來說比任何內容都更加美觀。

來修復你的MySQL的東西,並使用mysqli的,請嘗試以下操作:

$sql = "SELECT * FROM templates"; 
$result = $mysqli_connection->query($sql); 
while ($row = $result->fetch_array(MYSQLI_ASSOC)) { 
    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>"; 
} 

並嘗試使用以淨化你的信息:

$my_stuff = mysqli_connection->real_escape_string($row['that_stuff']); 

而且,你知道你可以使用單引號('')圍繞你的回聲語句,如果你想的話,對吧?可以更容易地轉義所有的雙引號。

因此,在充分,這可能是一個粗略的例子我會做什麼,但我想它分解成各種功能,也許創造一些全局變量(如連接本身):

<?php 


    $mysqli_connection = new mysqli("localhost", "username", "password", "database"); 
    $sql = "SELECT * FROM templates WHERE id=$id"; 
    $result = $mysqli_connection->query($sql); 
    // I'm assuming there is only one entry, so no while loop for me 
    $row = $result->fetch_array(MYSQLI_ASSOC); 

    $your_title = $mysqli_connection->real_escape_string($row['title']); 
    $path_to_image = $mysqli_connection->real_escape_string($row['image']); 
    $description = $mysqli_connection->real_escape_string($row['description']); 
    $price = $mysqli_connection->real_escape_string($row['price']); 

?> 

<html> 
    <head></head> 
    <body> 
     <h3><?php echo $your_title; ?></h3> 
     <img src="<?php echo $path_to_image; ?>" /> 
     <ul> 
      <li>Description: <?php echo $description; ?></li> 
      <li>Price: <?php echo $price; ?></li> 
      <!-- etc..--> 
+0

謝謝,但是,這不是我遇到的問題。我無法將所選ID的信息打到下一頁... –

+0

@TyBailey我編輯了我的答案。我認爲你有問題,因爲你需要在HTML呈現時回顯值 – burmat

+0

我試過你的方法,它似乎也不工作。我很確定問題是ID沒有被帶到URL。它只是在它的末尾添加了?id =沒有ID#... –