gpt4 book ai didi

php - 连接基于 sql id 的整数以用于 html ID 标记

转载 作者:行者123 更新时间:2023-11-28 01:13:04 24 4
gpt4 key购买 nike

作为我类(class)的一部分,我目前正在构建一个网站。该网站是一个电子商务网站。我将产品存储在 SQL 数据库中,然后使用 php 脚本将每一行回显到表中。我设法做到了这一点,但是,我现在希望在每个产品上添加一个按钮,该按钮将包含一个 href,用于将所选产品添加到购物车。唯一的问题是,要构建我需要 ID 的购物车数组,并且要在循环中设置 ID,你必须连接一个整数,我不知道如何在我的脚本中执行此操作,这是我目前拥有的,任何帮助将不胜感激。

    while ($row = mysqli_fetch_assoc($result)){
echo "<tr>";
echo "<td><br>".$row['Name']."</td><br>";
echo "<br>";
echo "<td><img src=".$row['Image']."height='200' width = '200'"."</td><br>";
echo "<br>";
echo "<td>Price: £".$row['Price']."</td><br>";
echo "<button type='button'><a href='test.php' id=>Buy</a></button>";
echo "</tr>";

提前致谢,我希望我已经提供了足够的细节。

最佳答案

有几种方法可以解决这个问题。

$id = 123; // as an example. Use the appropriate variable for this.

echo "<button type='button'><a href='test.php' id=$id>Buy</a></button>";

echo "<br>";

echo "<button type='button'><a href='test.php' id=\"$id\">Buy</a></button>";

HTML 源代码显示:

<button type='button'><a href='test.php' id=123>Buy</a></button><br><button type='button'>

<a href='test.php' id="123">Buy</a></button>

取决于您是否要引用它们,如果这就是问题所在。

或者:

echo "<button type='button'><a href='test.php' id='".$id."'>Buy</a></button>";

显示 HTML 源代码:

<button type='button'><a href='test.php' id='123'>Buy</a></button>

然后您可以使用 GET 数组提取数据。

即:

$var = $_GET['id'];

然后:

echo "<button type='button'><a href='test.php?id=".$id."'>Buy</a></button>";

HTML 来源:

<button type='button'><a href='test.php?id=123'>Buy</a></button>

在您的 while 循环中,您可以为 id 分配一个变量。

即:

$id = $row['id'];

我必须注意,如果您的数据包含单引号,使用单引号可能会产生不利影响,因此最好使用双引号。

即:(和转义双引号)

echo "<button type=\"button\"><a href=\"test.php?id=".$id."\">Buy</a></button>";

在 HTML 源代码中生成:

<button type="button"><a href="test.php?id=123">Buy</a></button>

实际上,上周我亲 body 验了这一点,其中 HTML 源代码显示了有关格式错误的 HTML 标记的警告,其中包含 Mark's Bar & Grill 等数据。


while ($row = mysqli_fetch_assoc($result)){

$id = $row['id']; // assuming you have a row for id.

// all your other code

echo "<button type='button'><a href='test.php?id=".$id."'>Buy</a></button>";

}

例如:

$name = "Bob's Bar & Grill";

echo "<button type='button'><a href='test.php' name='".$name."'>Buy</a></button>";

会在 HTML 源代码中产生一个 No space between attributes 警告。

来自 HTML 源代码:

<button type='button'><a href='test.php' name='Bob's Bar & Grill'>Buy</a></button>

如果与 CSS 规则一起使用,可能会产生不利影响。


这将与名称 $row['Name'] 等的另一行相关

  • 对此要小心。

关于php - 连接基于 sql id 的整数以用于 html ID 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36813984/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com