對於一個學校項目,我試圖用MySQLi創建一個只有php的shoppingcart。爲此我有一個名爲index.php的目錄。在這是與產品的表和每個產品後有一個按鈕,應該將項目添加到購物車。PHP按鈕href不工作
唯一的問題是我無法正常工作。
<?php
session_start();
include 'connect.php';
$qry = "select * from products";
$result = mysqli_query($connect, $qry);
echo "<table class='catalogue'>";
echo "<tr><th>ID</th><th>Code</th><th>Name</th><th>Description</th><th>Image</th></th><th>Price</th><th>Buy</th></tr>";
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo "<tr><td>";
echo $row['id'];
echo "</td><td>";
echo $row['product_code'];
echo "</td><td>";
echo $row['product_name'];
echo "</td><td>";
echo $row['product_desc'];
echo "</td><td>";
echo $row['product_img_name'];
echo "</td><td>";
echo $row['price'];
echo "</td><td>";
echo "<input type='submit' value='Add' href='cart.php?id=['id']'/>";
echo "</td></tr>";
}
echo "</table>";
?>
cart.php看起來像這樣。
<?php
session_start();
require 'connect.php';
require 'item.php';
$result = mysqli_query($connect, 'select * from products where id='.$_GET['id']);
$product = mysqli_fetch_object($result);
if(isset($_GET['id'])){
$item = new Item();
$item->id = $product->id;
$item->name = $product->product_name;
$item->price = $product->price;
$item->quantity = 1;
$_SESSION['cart'][] = $item;
}
echo "<table class='cart'>";
echo "<tr><th>ID</th><th>Name</th><th>Price</th><th>Quantity</th><th>Sub Total</th></tr>";
$cart = unserialize(serialize($_SESSION['cart']));
for($i=0; $i<count($cart); $i++){
echo "<tr><td>";
echo $cart[$i]->id;
echo "</td><td>";
echo $cart[$i]->product_name;
echo "</td><td>";
echo $cart[$i]->price;
echo "</td><td>";
echo $cart[$i]->quantity;
echo "</td><td>";
echo $cart[$i]->price * $cart[$i]->quantity;
echo "</td></tr>";
}
?>
請原諒您可能會看到的任何其他錯誤,我對PHP比較陌生。
按鈕沒有hrefs。查看HTML表單上的教程。 – developerwjk
這個'select * from產品的id ='。$ _ GET ['id']'打開你的SQL注入。用準備好的語句將用戶輸入與SQL分離。 – chris85
'echo「Add」;' – Steve