简体   繁体   English

PHP按钮href不起作用

[英]PHP Button href not working

For a school project I am trying to create a shoppingcart with php only with MySQLi. 对于一个学校项目,我试图用MySQL只用MySQL创建一个购物车。 For this I have a catalogue called index.php. 为此,我有一个名为index.php的目录。 In this is a table with the product and after every product there is a button which should add the item to the shoppingcart. 这是一个包含产品的表格,每个产品后面都有一个按钮,可以将商品添加到购物车中。

The only problem is that I cannot get the link working properly. 唯一的问题是我无法使链接正常工作。

    <?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>";
    ?>

The cart.php looks like this. 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>";
    }
?>  

Please forgive any other mistakes you might see, I am rather new to PHP. 请原谅你可能看到的任何其他错误,我对PHP很新。

Buttons don't have hrefs, anchors( <a> ) do, so using an anchor it would be 按钮没有hrefs,锚点( <a> ),所以使用锚点就可以了

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

you could always style it like a button if you want it to look like one. 如果你希望它看起来像一个按钮,你可以像按钮一样设置样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM