如何在每行上添加按钮

时间:2019-05-14 03:27:35

标签: php mysql

我正在使用php和mysql做一个简单的购物车。我可以显示整个数据库,但是如何在每行上添加一个按钮;例如“添加到购物车”?

    <?php

$link = mysqli_connect("rerun","potiro","pcXZb(kL","poti");
if (!$link)
   die("Could not connect to Server");


$query_string = "select * from products";
$result = mysqli_query($link,$query_string);

$num_rows = mysqli_num_rows($result);

if ($num_rows > 0 ) {
    print "<table border=0>";
    while ( $a_row = mysqli_fetch_row($result) ) {
         print "<tr>\n";
         foreach ($a_row as $field)
             print "\t<td>$field</td>\n";
         print "</tr>";
    }
    print "</table>";
}

mysqli_close($link);
?>

4 个答案:

答案 0 :(得分:1)

在$ field列之后添加新列,只需将添加到购物车按钮链接到添加到购物车页面,然后放置产品ID ,这样,如果您在该行上按添加到购物车按钮,该行上的产品ID将得到确认,并且使用此产品ID可以在购物车页面中获取产品信息并确认订单。

OnPost

答案 1 :(得分:0)

这样的按钮将结合HTML和CSS来制作;您只需要使用PHP在循环中echo除去代码即可。

while ( $a_row = mysqli_fetch_row($result) ) {
     print "<tr>\n";
     foreach ($a_row as $field)
         print "\t<td>$field</td>\n";
         print "\t<td><a href='process.php?id=$field'>Add to cart</a></td>\n";
     print "</tr>";
}

答案 2 :(得分:0)

只需在每行上添加另一列。

print "<table border=0>";
while ( $a_row = mysqli_fetch_row($result) ) {
     print "<tr>\n";
     foreach ($a_row as $field)
         print "\t<td>$field</td><td><button>Add to cart</button>\n";
     print "</tr>";
}
print "</table>";

答案 3 :(得分:0)

在foreach循环中添加的代码是

    foreach ($a_row as $field)
       print "\t<td>$field</td><td><button>Add to cart</button>\n";//Add button here or any thing you want just add it
  }

希望这对您有帮助

谢谢