PHP按钮href不起作用

时间:2015-12-14 22:35:03

标签: php button mysqli href

对于一个学校项目,我试图用MySQL只用MySQL创建一个购物车。为此,我有一个名为index.php的目录。这是一个包含产品的表格,每个产品后面都有一个按钮,可以将商品添加到购物车中。

唯一的问题是我无法使链接正常工作。

    <?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很新。

1 个答案:

答案 0 :(得分:6)

按钮没有href,锚点(<a>)这样做,所以使用锚点就是

echo "<a href='cart.php?id=$row[id]'/>Add</a>";

如果您希望它看起来像一个按钮,您可以像按钮一样设置样式。