Php 基于sql id连接整数以在html id标记中使用
我目前正在建立一个网站,作为我课程的一部分。该网站是一个电子商务网站。我将产品存储在一个SQL数据库中,然后使用php脚本将每一行返回到一个表中。我已经设法做到了这一点,但是,我现在希望在每个产品上添加一个按钮,该按钮将包含一个href,用于将所选产品添加到购物车中。唯一的问题是,要构建cart数组,我需要ID,要在循环中设置ID,你必须连接一个整数,我不知道如何在我的脚本中实现这一点,这是我目前拥有的,任何帮助都将不胜感激Php 基于sql id连接整数以在html id标记中使用,php,html,mysql,sql,Php,Html,Mysql,Sql,我目前正在建立一个网站,作为我课程的一部分。该网站是一个电子商务网站。我将产品存储在一个SQL数据库中,然后使用php脚本将每一行返回到一个表中。我已经设法做到了这一点,但是,我现在希望在每个产品上添加一个按钮,该按钮将包含一个href,用于将所选产品添加到购物车中。唯一的问题是,要构建cart数组,我需要ID,要在循环中设置ID,你必须连接一个整数,我不知道如何在我的脚本中实现这一点,这是我目前拥有的,任何帮助都将不胜感激 while ($row = mysqli_fetch_asso
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>";
while($row=mysqli\u fetch\u assoc($result)){
回声“;
回显“
”$row['Name']。“
”;
回声“
”;
回声“
”;
回声“
”;
echo“价格:英镑”。$row[“价格]。”
“;
回声“;
回声“;
}
提前谢谢,我希望我已经提供了足够的细节 有几种方法可以做到这一点
$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>";
然后:
我必须注意如果您的数据包含单引号,则使用单引号可能会产生不利影响,因此最好使用双引号
即:(和转义双引号)
例如:
$name = "Bob's Bar & Grill";
echo "<button type='button'><a href='test.php' name='".$name."'>Buy</a></button>";
如果与CSS规则一起使用,可能会产生不利影响
这将与名称
$row['name']
等的另一行相关
- 小心点
- 不要担心它是一个整数。只需像处理字符串一样使用从db返回的值。它将从int转换为string。还要注意的是,实际上并不需要为创建字符串而进行的连接。由于php变量以$开头,您可以像这样内联编写它们:
echo "<button type='button'><a href=\"test.php\" id=\"$row['id']\">Buy</a></button>";
echo”“;
非常感谢,这为我整理了代码,因为我在另一个PHP脚本中使用了$\u GET['id')。@dannstuff不客气,很高兴我能帮上忙。你看到我关于使用单引号的说明了吗?上周我自己也遇到了一个问题,需要使用双引号。我想这是第一次;-)像我老爸常说的那样生活和学习。干杯
<button type='button'><a href='test.php' id='123'>Buy</a></button>
$var = $_GET['id'];
echo "<button type='button'><a href='test.php?id=".$id."'>Buy</a></button>";
<button type='button'><a href='test.php?id=123'>Buy</a></button>
$id = $row['id'];
echo "<button type=\"button\"><a href=\"test.php?id=".$id."\">Buy</a></button>";
<button type="button"><a href="test.php?id=123">Buy</a></button>
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>";
<button type='button'><a href='test.php' name='Bob's Bar & Grill'>Buy</a></button>
echo "<button type='button'><a href=\"test.php\" id=\"$row['id']\">Buy</a></button>";