有几种方法可以解决这个问题。
$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>
实际上,我上周亲身体验了这一点,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'] 等的另一行有关。