我有一个显示产品的页面,当我点击特定项目并将其传递到另一个页面时,我想获得产品ID。
我可以知道如何实现这一目标?
我总是得到最后一个PID,我的代码:
<head>
<title>Toy-All</title>
<!--Wilmos: Using external CSS File to format the page style and fonts.-->
<link href="StyleSheet2.css" rel="Stylesheet" type="text/css" />
</head>
<body>
<form method = "post" action "getpid.php">
<div class="stylediv2-Middle-Toy-all">
<div class="transbox-Toy-all">
<?php
//open connection to MySQL Server
$connection = mysql_connect('localhost','root', '')
or die ('Unable to connect to !');
// select database for use
mysql_select_db('we-toys') or die ('Unable to select database!');
$query = 'SELECT p.*, price.priceN FROM product p, pricing price WHERE p.pid = price.pid and p.PGroup = 1 and p.PType = 1';
$result = mysql_query($query)
or die ('Error in query: $query. ' . mysql_error());
if (mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_array($result))
{
echo '<div style="float:left;margin-right: 10px; margin-left: 10px;"><img src="'.$row[5].'" width=200px height=200px; ?> </div>
<h3>'.$row[1].'</h3>
<h1><span style="color:red"> $ '.$row[7].' </span>
<input type="hidden" name="pid" value= '.$row[0].' >
<input id="AddtoCart-Btn" type="Submit" value= "Add to Cart" >
</h1>
';
}
}
else
{
echo "No rows found!";
}
mysql_free_result($result);
mysql_close($connection);
?>
</div>
</div>
</form>
</body>
</html>
答案 0 :(得分:2)
如果您从$_SESSION['PID']
检索数据,那么您将始终获得最后一个ID,因为您会继续为该会话重新分配新值。
您可以通过指向另一个PHP页面的链接来实现此目的。例如:
<a href='anotherPage.php?id=<?php echo $row[0]; ?>'>Add to Cart</a>
根据要求提供更完整的代码
<?php
$query = 'SELECT p.*, price.priceN FROM product p, pricing price
WHERE p.pid = price.pid and p.PGroup = 1 and p.PType = 1';
$result = mysql_query($query)
or die ('Error in query: $query. ' . mysql_error());
?>
<?php while ($row = mysql_fetch_array($result)) { ?>
<h3><?php echo $row[1]; ?></h3>
<a href='anotherPage.php?id=<?php echo $row[0]; ?>'>Add to Cart</a><br><br>
<?php } ?>
适用于anotherPage.php
代码
<?php
echo "You are trying to add this product ID to cart: " . $_GET['id'];
?>
答案 1 :(得分:1)
您可以使用我在此代码中提供的此表单。
$query = 'SELECT p.*, price.priceN FROM product p, pricing price WHERE p.pid = price.pid and p.PGroup = 1 and p.PType = 1';
$result = mysql_query($query)
or die ('Error in query: $query. ' . mysql_error());
if (mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_array($result))
{
$pid = ($row[0]);
$_SESSION['PID'] = $pid;
echo '<div style="float:left;margin-right: 10px; margin-left: 10px;"><img src="'.$row[5].'" width=200px height=200px; ?> </div>
<h3>'.$row[1].'</h3>
<h1><span style="color:red"> $ '.$row[7].' </span>
<form method="post" action="cart.php">
<input type="hidden" name="pid" value= '.$row[0].' >
<input id="AddtoCart-Btn" type="Submit" value= "Add to Cart" >
</form>
$pid = '.$row[0].';
</h1>
';
}
}
现在你应该创建一个新页面,例如cart.php
echo $_POST['pid'];
答案 2 :(得分:0)
如果我理解正确,以下情况应该有效:
<form method="post" action="anotherPage.php">
<input type="hidden" name="id" value="<?php echo "$row[0]"?>"/>
<input id="AddtoCart-Btn" type="Submit" value="<?php echo "$row[0]" ?>" />
</form>
所以基本上当您点击产品按钮时,可以在anotherPage.php
修改强>
我重写了您的代码以提高可读性:
<div style="float:left;margin-right: 10px; margin-left: 10px;"><img src=<?php echo $row[5]; ?> width=200px height=200px; ?> </div>
<h3><?php echo $row[1]; ?></h3>
<h1>
<span style="color:red"> $ <?php echo $row[7] ?></span>
<form method="post" action="cart.php">
<input type="hidden" name="pid" value=<?php $row[0]; ?> >
<input id="AddtoCart-Btn" type="Submit" value= "Add to Cart" >
</form>
<?php $pid = $row[0]; ?>
</h1>
尽可能避免回送大量的HTML。立即尝试,如果失败则提供错误消息。
以上情况,下面的简单测试实现了什么:
<?php
$id = 1;
if (isset($_POST['submit_btn'])){
echo $_POST['id'];
}
?>
<form method="post" action="#">
<input type="hidden" name="id" value= <?php echo $id; ?> >
<input type="submit" name="submit_btn" value="submit">
</form>