从mysql db获取值并将其发布到php页面

时间:2012-05-30 19:10:06

标签: php mysql html sql

我从数据库获取值并将其发布到另一个页面时遇到一些问题,这是我的代码。在search.php上,用户输入价格最小值和价格最大值,然后在页面上result.php它从数据库中显示产品的名称和价格以及更多信息的链接。(info.php)此第3页将显示一些额外的基于result.php上显示的产品ID的信息 我试图从结果中隐藏Id_product的值并将其发布到第三种形式但不起作用。我收到错误提示:未定义的索引:在info.php第4行中的id

//////search.php /////

<form  method="post" action="result.php" > 
<tr> <INPUT type=text size=20 name=pricemin ><BR>  </tr>  
<tr> <INPUT type=text size=20 name=pricemax ><BR>  </tr>
<tr> <input type="submit" class="Nom" id="button" value="Valider" /></tr></form>

///////result.php //////

<?php include 'includes/connection.php';
$pricemin = $_POST['pricemin'];
$pricemax = $_POST['pricemax'];
$query = "SELECT * FROM product where price between '$pricemin' and '$pricemax'";
$result = mysql_query($query);?>
<?php if(mysql_num_rows($result) > 0)
while($products = mysql_fetch_array($result)) {?>   
<?php echo $products['name_product'] ;echo $products['price_product'] ;
echo "<a href='http://localhost/mywebsite/info.php'>More infos</a>"
?>
<form method="post" action="info.php" >
<input type="hidden" name="id" value="<?php echo $products['id_product']; ?>" />
</form><?php } ?>...

///// info.php //////

<?php
include 'includes/connection.php';
$id_product = $_POST['id'];
$query = "SELECT * FROM product where id_product='$id_product'";
$result = mysql_query($query);   ?>
<html><head><title>.. </title></head><body>
<?php 
while($products = mysql_fetch_array($result)) {?>
<h1>Product ID : <?php echo  $products['id_product'] ; ?> </h1>
<h1>Product name : <?php echo  $products['name_product'] ; ?> </h1>
<h1>Product Qt : <?php echo  $products['quantity_product'] ; ?> </h1>
<h1>Product Spec : <?php echo  $products['spec_product'] ; ?> </h1>
<?php } ?></body></html>

1 个答案:

答案 0 :(得分:1)

试试这个:

//////search.php /////

<form  method="post" action="result.php" > 
<tr> <INPUT type=text size=20 name=pricemin ><BR>  </tr>  
<tr> <INPUT type=text size=20 name=pricemax ><BR>  </tr>
<tr> <input type="submit" class="Nom" id="button" value="Valider" /></tr></form>

///////result.php //////

<?php include 'includes/connection.php';
$pricemin = $_POST['pricemin '];
$pricemax = $_POST['pricemax '];
$query = "SELECT * FROM product where price between '".$pricemin."' and '".$pricemax."'";
$result = mysql_query($query);

if(mysql_num_rows($result) > 0)
while($products = mysql_fetch_array($result)) {
$id = $products['id_product'];
echo $products['name_product'] ;echo $products['price_product'] ;
echo "<a href='info.php?id=";
echo $id;
echo "'>More infos</a>"
?>

<?php } ?>...

///// info.php //////

<?php
include 'includes/connection.php';
$id_product = $_GET['id'];
$query = "SELECT * FROM bien where id_product= '".$id_product."'";
$result = mysql_query($query);   ?>
<html><head><title>.. </title></head><body>
<?php 
while($products = mysql_fetch_array($result)) {?>
<h1>Product ID : <?php echo  $products['id_product'] ; ?> </h1>
<h1>Product name : <?php echo  $products['name_product'] ; ?> </h1>
<h1>Product Qt : <?php echo  $products['quantity_product'] ; ?> </h1>
<h1>Product Spec : <?php echo  $products['spec_product'] ; ?> </h1>
<?php } ?></body></html>

这应该将ID传递给info.php

表单只有在您提交按钮时才有效,所以如果您点击链接,除非您使用JS onsubmit功能,否则表单将不会提交。