2012-12-07 104 views
0

我正在一個迷你購物車上工作。一切似乎都奏效,但是,當我點擊不同的產品時,它不會提取正確的數據。迷你購物車遇到問題

我選擇產品1 - 它給了我正確的信息,但如果我選擇第2種產品,它會給我同樣的信息產品1

我下面會顯示我的代碼,但我相信錯誤來了從下面這行代碼:

$sql = sprintf("SELECT Model, Price FROM Bike WHERE BikeCode = %d;", $bikecode); 

我不認爲它得到$ bikecode

這是PHP文件內整個代碼:

<?php 
    $bikecode = $_GET['id'];  //the product id from the URL 
    $action = $_GET['action']; //the action from the URL 

if($bikecode && !productExists($bikecode)) { 
    die("Product Doesn't Exist"); 
} 

    switch($action) { //decide what to do 

     case "add": 
      $_SESSION['cart'][$bikecode]++; //add one to the quantity of the product with id $bikecode 
     break; 

     case "remove": 
      $_SESSION['cart'][$bikecode]--; //remove one from the quantity of the product with id $bikecode 
      if($_SESSION['cart'][$bikecode] == 0) unset($_SESSION['cart'][$bikecode]); //if the quantity is zero, remove it completely (using the 'unset' function) - otherwise is will show zero, then -1, -2 etc when the user keeps removing items. 
     break; 

     case "empty": 
      unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart. 
     break; 
    } 

    if($_SESSION['cart']){ 

     echo "<table width=\"100%\">"; 

      foreach($_SESSION['cart'] as $bikecode => $quantity) { 

       $sql = sprintf("SELECT Model, Price FROM Bike WHERE BikeCode = %d;", $bikecode); 

       $result = mysqli_query($con, $sql); 

       if(mysqli_num_rows($result) > 0) { 

        list($model, $price) = mysqli_fetch_row($result); 

        $cost = $quantity * $price; 
        $total = $total + $cost; 

         echo "<tr><th>Model:</th><th>Quantity:</th><th>Price:</th></tr>"; 
         echo "<tr>"; 
         echo "<td align=\"center\">$model</td>"; 
         echo "<td align=\"center\">$quantity <a href=\"$_SERVER[PHP_SELF]?action=remove&id=$bikecode\">X</a></td>"; 
         echo "<td align=\"center\">£$cost</td>"; 
        echo "</tr>"; 
       } 
      } 

      echo "<tr>"; 
       echo "<td colspan=\"2\" align=\"right\">Total</td>"; 
       echo "<td align=\"right\">£$total</td>"; 
      echo "</tr>"; 

      echo "<tr>"; 
       echo "<td colspan=\"3\" align=\"right\"><a href=\"$_SERVER[PHP_SELF]?action=empty\" onclick=\"return confirm('Are you sure?');\">Empty Cart</a></td>"; 
      echo "</tr>";  
     echo "</table>"; 

    }else{ 
     echo "You have no items in your shopping cart."; 
    } 

function productExists($bikecode) { 
    $sql = sprintf("SELECT * FROM Bike WHERE BikeCode = %d;", $bikecode); 
    return mysqli_num_rows(mysqli_query($con, $sql)) > 0; 
} 
?> 

幫助將非常讚賞,但如果你不能找到一個錯誤,你可以請直接與我顯示此代碼的另一種方式:

$sql = sprintf("SELECT Model, Price FROM Bike WHERE BikeCode = %d;", $bikecode); 
+0

您是否在將它包含在查詢中之前明確將$ bikecode轉換爲'int'?即:'$ sql = sprintf(「SELECT Model,Price FROM Bike WHERE BikeCode =%d;」,intval($ bikecode));'除此之外,'echo'查詢到頁面並檢查它是否存在形成你期望的方式。 – Sammitch

+0

我的$ bikecode不是一個整數其varchar我的自行車代碼包含文本和數字。 – Hii

+1

那麼你爲什麼在'sprintf()'中使用'%d'標誌,爲什麼它沒有用查詢中的引號包裝呢?使用這個:'sprintf(「SELECT Model,Price FROM Bike WHERE BikeCode ='%s';」,$ bikecode);' – Sammitch

回答

0

它看起來像你說對了,但爲便於參考,以其他可能搜索它的問題是,當%d被視爲整數時,您正在使用sprintf()中的%d來嘗試並表示一個varchar。使用%s將解決問題。

sprintf() - Manual