【问题标题】:PHP Button href not workingPHP按钮href不起作用
【发布时间】:2016-03-20 13:46:51
【问题描述】:

对于一个学校项目,我正在尝试仅使用 MySQLi 创建一个带有 php 的购物车。为此,我有一个名为 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 比较陌生。

【问题讨论】:

  • 按钮没有href。请参阅有关 HTML 表单的教程。
  • 这个select * from products where id='.$_GET['id'] 向您打开SQL 注入。使用准备好的语句将用户输入与 SQL 分开。
  • echo "&lt;a href='cart.php?id=" . $row['id'] . "'&gt;Add&lt;/a&gt;";

标签: php button mysqli href


【解决方案1】:

按钮没有href,锚(&lt;a&gt;)有,所以使用锚就可以了

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

如果你想让它看起来像一个按钮,你总是可以把它设计成一个按钮。

【讨论】:

  • ^^^ 请务必检查此答案为正确答案
猜你喜欢
  • 2016-06-19
  • 2012-09-03
  • 1970-01-01
  • 2015-05-27
  • 2013-08-30
  • 2018-07-11
  • 2019-12-06
  • 1970-01-01
  • 2012-07-15
相关资源
最近更新 更多