2015-09-27 56 views
1

我在執行php會話購物車時遇到問題。我從數據庫中獲得產品列表,並且每個都有自己的「添加到購物車」按鈕。問題是如何通過點擊按鈕添加到會話中來獲取它們中的任何一個?PHP,按名稱獲取按鈕,其中包含腳本

<?php session_start();</br> 
include_once 'dbconnect.php'; 
$_SESSION['cart']= array(); 
$sql="SELECT * FROM products ORDER BY name ASC"; 
$query=mysql_query($sql);?> 
<a href="cart-display.php">Go To Cart</a> 

我開始一個循環來輸出的所有產品數據庫

<?php 
while($row=mysql_fetch_array($query)){ 
?> 

<form method="get"> 
<tr><td><?php echo $row['name'] ?></td> 
<td><?php echo $row['description'] ?></td> 
<td><?php echo $row['price'] ?></td> 
<td><?php echo $elId= $row['id_product']?></td> 
<td><button type="submit" name="btn-submit.<?php echo $elId;?>">Add To Chart</button></td> 
</tr><br> 
</form> 

然後我嘗試$ _GET []讓其中任何一個,然後嘗試推入陣列會議。但是由於循環,它只增加了ast產品。請幫助

<?php } 

if(isset($_GET['btn-submit.$elId'])){ 
array_push($_SESSION['cart'], $elId);}?> 
+0

'< ?php session_start();
'whaat。 _deprecated_ mysql函數和在PHP中不正確地使用HTML。 '$ query = mysql_query($ sql);?> Go To Cart'這些工作是否有效? – user5173426

+0

'$ _GET ['btn-submit。$ elId']'這是什麼?這是jQuery選擇器?大聲笑 – aldrin27

+0

圍繞最後一個陳述有什麼樣的循環?換句話說,你明確地循環?這是沒有顯示在你的代碼... – Floris

回答

1

你不得不重新考慮該腳本:對每個頁面加載3行,你完全清空$ _SESSION [車]

  • 您使用的是過時的功能:MySQL的
  • 您使用不必要的「形式」標籤
  • 您檢查$ _GET輸入僅在最後一個產品,一個壞的方式

這裏是一個開始:

<?php 
session_start(); 
// initialize session var only ONE TIME 
if (!isset($_SESSION["cart"])) { 
    $_SESSION["cart"]= array(); 
} 
// store a new element ID 
$elId=""; 
// check $_GET input and validate it 
if(isset($_GET["elId"])){ 
    // !! filter $_GET data somehow (e.g. make sure it is_numeric) 
    $elId=preg_replace("/[^0-9]/","",$_GET["elId"]); 
    // you could check in DB if the $elId is a valid value 
} 
// update SESSION if elId is not empty after validating $_GET 
if (!empty($elId)) { 
    // if not already in array, initialize field 
    /* 
    use a multidimensional array to count how many products 
    of one type are in the cart, eg: $_SESSION[cart][343]=3; 
    aka Three products with id: 343 
    */ 
    if (!isset($_SESSION["cart"][$elId])) { 
     $_SESSION["cart"][$elId]=0; 
    } 
    // update cart 
    $_SESSION["cart"][$elId]++; 
    // ! user should be alerted, e.g: 
    echo "<p>New product added to cart!</p>"; 
} 
// DB access 
//include_once 'dbconnect.php'; 
require "dbconnect.php"; 
$sql="SELECT * FROM products ORDER BY name ASC"; 
// fetch results 
// $query=mysql_query($sql); 
/* !! USE mysqli INSTEAD !! in file dbconnect.php */ 
$conn = dbconnect(); 
$results = $conn->query($sql); 
// start products' HTML 
echo "<a href='cart-display.php'>Go To Cart</a>"; 
echo "<table>"; 
// again, use mysqli 
// while($row=mysql_fetch_array($query)){ 
while($row = $results->fetch_array()) { 
    // * <form> tags are unnecessary, <a> tags are sufficient 
    $elId=$row["id_product"]; 
    echo " 
     <tr> 
     <td>$row[name]</td> 
     <td>$row[description]</td> 
     <td>$row[price]</td> 
     <td>$row[id_product]</td> 
     <td><a href='?elId=$elId'>Add to cart</a></td> 
     </tr> 
    "; 
} 
echo "</table>"; 

?> 

而dbconnect.php:

<?php 
function dbconnect() { 
    static $conn; 
    if (!isset($conn)) { 
     $conn = mysqli_connect('localhost','username','password','dbname'); 
    } 
    if ($conn === false) { 
     return mysqli_connect_error(); 
    } 
    return $conn; 
} 
?> 
+0

非常感謝你!) –

+0

乾杯!我希望它提供了你正在尋找的答案。 – verjas