2013-04-29 63 views
0

我有以下問題。我在下面的陣列會話中存儲購物車提取陣列車php

session_start(); 
$id= $_GET['id']; 
if(isset($_SESSION['cart'])) 
{ 
array_push($_SESSION['cart'], $id); 
} 
else 
    $_SESSION['cart']= array($id); 

header("location:cart.php"); 

而且當我嘗試檢索購物車時。我拿到與放在購物車上相同的產品編號。

<?php 
if(!isset($_SESSION['cart'])) { 
    echo "Your cart is empty.<br /><br /><a href='products.php'>Show products</a>"; 
} else { 
    echo '<table border="0.2">'; 

    $total_price = 0; 

    foreach($_SESSION['cart'] as $id) { 
     $the_query = "select * from products where id='$id' GROUP BY id"; 

     $result = mysql_query($the_query) or die('Query failed: ' . mysql_error()); 

     $the_product = mysql_fetch_array($result, MYSQL_ASSOC); 

     $total_price = $total_price + $the_product['price']; 

     $href = "show_products.php?id=".$the_product['id']; 
     //echo "<tr>"; 
     echo "<tr><td><a href='$href'>"; 
     echo "<img src='".$the_product['image_url_small']."' /></a></td>"; 
     echo "<td><strong>".$the_product['name']."</strong></td><td><em>$".$the_product['price']."</em>"; 
     echo "</td>"; 
     echo "<td> <a href='do_deletecart.php?id=". $the_product['id'] ."'>Delete item </a></td></tr>"; 
    } 
    echo "<tr><td colspan='2'></td></tr>"; 
    echo "<tr><td style='text-align:center;font-size:40px;'>$</td><td><strong>Total</strong><br /><em>$".$total_price."</em></td></tr>"; 
    echo "</table>"; 
    echo "<br /><a href='empty_cart.php'>Empty Cart</a> <a href='showallproducts.php'>Show phones</a><br /><br />"; 
} 

我怎樣才能讓它只顯示一個產品ID或名稱。在此先感謝

+1

此代碼非常容易受到[SQL注入](http://bobby-tables.com)的影響。請清理您的輸入並使用[近期API](http://j.mp/PoWehJ) – Touki 2013-04-29 10:20:10

+0

我實際上是PHP新手,感謝您的建議。 – user2210209 2013-04-30 08:33:59

回答

1

如果我正確理解你的問題,你會得到許多相同產品ID的結果。這是因爲您在$_SESSION變量中多次存儲相同的ID值。

您可以執行以下操作,以避免在$_SESSION變量中重複相同的ID。

編輯

爲了完整起見,我已經更新了代碼。希望有所幫助。

的index.php

<?php 

session_start(); 

$id= isset($_GET['id']) ? $_GET['id'] : null; 

if(!is_null($id)){ 
    if(isset($_SESSION['cart']) && count($_SESSION['cart']) > 0){ 

     // increment product quantity if already exists 
     // or create a new one 
     add_or_increment_product_to_cart($id, $_SESSION['cart']); 

    } else { 
     // initialize cart 
     // add the first product 
     $_SESSION['cart'] = array(); 
     array_push($_SESSION['cart'], (object) array('id' => $id, 'quantity' => 1)); 
    } 
} 

function add_or_increment_product_to_cart($id, $cart){ 

    foreach ($cart as $key => $product) { 
     if($id == $product->id){ 
      $product->quantity++; 
      return; 
     } 
    } 

    array_push($_SESSION['cart'], (object) array('id' => $id, 'quantity' => 1)); 
} 

header("location:cart.php"); 

Cart.php

<?php 

session_start(); 

$cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : null; 

if($cart) { 
    foreach ($cart as $key => $product) { 
     $the_query = "SELECT * FROM products WHERE id=" . $product->id . " LIMIT 1"; 

     // your code to fetch the products from the database 
     // what you have done is fine but vulnerable 
     // PDO recommended 
    } 
} else { 
    echo "Your cart is empty.<br /><br /><a href='products.php'>Show products</a>"; 
} 

同時請注意,mysql_connect被棄用,PDO類是連接到數據庫的建議和安全的方式。 @Touki在他的評論中說,你的代碼容易受到SQL注入的攻擊。

+0

使用'array()'可以工作,但仍然有點麻煩。我建議不要修復症狀,要解決問題的根源(同一產品ID的多個實例)。 – Aquillo 2013-04-29 11:24:20

+0

是的。我同意。 'in_array()'檢查確保不存在相同ID的重複實例。 – Subash 2013-04-29 11:50:02

+0

我的錯誤,我沒有讀你的例子正確。此示例用於注入任何新的「id」,而不是循環打印它們。 – Aquillo 2013-04-29 11:52:53

0

我建議只執行一個查詢來檢索所有產品,然後遍歷查詢的結果以填充HTML。例如;

$the_query = "select * from products where id in (". implode(',', $_SESSION['cart']) .")"; 
$result = mysql_query($the_query); 
while (($the_product = mysql_fetch_array($result, MYSQL_ASSOC))) { 
    ... 
} 

這有額外的好處,你只執行一個查詢,也只會選擇每行產品一行。

但值得注意的是,mysql_ *方法已被廢棄,建議您開始使用另一個庫,如mysqli或PDO。

在相關說明中,此代碼當前非常容易進行SQL注入,理想情況下應該在輸入查詢字符串之前對輸入進行消毒。

+0

謝謝,我會試試看。 – user2210209 2013-04-30 08:33:19