下午大家,快速提问这个获取查询。
<?php
$sql = "SELECT * FROM products ";
if(isset($_POST['Submit'])){
if(empty($_POST['Search'])){
$error = true;
}else{
$searchq = mysql_real_escape_string($_POST['Search']);
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
$sql .= "WHERE type LIKE '%$searchq%' or name LIKE '%$searchq%'";
}
} $query = mysql_query($sql) or die(mysql_error());
$sql1 = "SELECT * FROM products ";
if(isset($_GET['q'])){
$categories = mysql_real_escape_string($_GET['q']);
$sql1 .= "WHERE type LIKE '%$categories%'";
} $query1 = mysql_query($sql1) or die(mysql_error());
?>
<?php while ($row = mysql_fetch_array($query) and $row = mysql_fetch_array($query1)) { ?>
<div class="prod_box">
<div class="top_prod_box"></div>
<div class="center_prod_box">
<div class="product_title"><a href="productsview.php<?php echo '?id='.$row['serial']; ?>"><b><?php echo $row['name']?></a></div>
<div class="product_img"><a href="productsview.php<?php echo '?id='.$row['serial']; ?>"><img src="<?php echo $row['picture']?>" height="100" width="120" /></a></div>
<div class="prod_price"><span class="reduce"><?php if($row['rprice'] > 0) {echo "£"; echo $row['rprice'];}?></span> <span class="price"><big style="color:green">£<?php echo $row['price']?></big></span></div>
</div>
<div class="bottom_prod_box"></div>
<div align="center" class="prod_details_tab"> <input type="button" value="Add to Cart" onclick="addtocart(<?php echo $row['serial']?>)" /></td> </div>
</div>
<?php } ?></p>
任何人都看错了吗?查询提取正常,但不正确,即使表单甚至没有提交,它也会获取其他查询的部分内容。 :\
答案 0 :(得分:2)
您可以一次运行查询。只需在创建查询时合并GET
和POST
。
$noWhere = true;
$sql = "SELECT * FROM products ";
if(isset($_POST['Submit'])){
if(empty($_POST['Search'])){
$error = true;
}else{
$searchq = mysql_real_escape_string($_POST['Search']);
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
$sql .= "WHERE type LIKE '%$searchq%' or name LIKE '%$searchq%'";
$noWhere = false;
}
}
// check if categories got sent
if(isset($_GET['q'])){
$categories = mysql_real_escape_string($_GET['q']);
if($noWhere) $sql .= "WHERE type LIKE '%$categories%'";
else $sql .= " OR type LIKE '%$categories%'";
}
$query = mysql_query($sql) or die(mysql_error());
?>
<?php while ($row = mysql_fetch_array($query)) { ?>
虽然我必须添加一个评论。您正在使用mysql()
函数,这些函数已被正式弃用。我应该检查PDO是否使用PHP中的db。
答案 1 :(得分:1)
尝试使用不同的获取变量:
<?php while ($row = mysql_fetch_assoc($query) and $row2 = mysql_fetch_assoc($query1)) { ?>
<div class="prod_box">
<div class="top_prod_box"></div>
<div class="center_prod_box">
<div class="product_title"><a href="productsview.php<?php echo '?id='.$row['serial']; ?>"><b><?php echo $row['name']?></a></div>
<div class="product_img"><a href="productsview.php<?php echo '?id='.$row['serial']; ?>"><img src="<?php echo $row['picture']?>" height="100" width="120" /></a></div>
<div class="prod_price"><span class="reduce"><?php if($row['rprice'] > 0) {echo "£"; echo $row['rprice'];}?></span> <span class="price"><big style="color:green">£<?php echo $row['price']?></big></span></div>
</div>
<div class="bottom_prod_box"></div>
<div align="center" class="prod_details_tab"> <input type="button" value="Add to Cart" onclick="addtocart(<?php echo $row['serial']?>)" /></td> </div>
</div>
<?php } ?></p>
例如,$row
用于第一个查询,$row2
用于第二个查询。
如果您仅想要$row
,则必须构建一个输出query1
和query2
首先在if语句和输出中运行查询。试试这段代码:
<?php
$sql = "SELECT * FROM products ";
if(isset($_POST['Submit'])){
if(empty($_POST['Search'])){
$error = true;
}else{
$searchq = mysql_real_escape_string($_POST['Search']);
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
$sql .= "WHERE type LIKE '%$searchq%' or name LIKE '%$searchq%'";
$query = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_assoc($query)) { ?>
<div class="prod_box">
<div class="top_prod_box"></div>
<div class="center_prod_box">
<div class="product_title"><a href="productsview.php<?php echo '?id='.$row['serial']; ?>"><b><?php echo $row['name']?></a></div>
<div class="product_img"><a href="productsview.php<?php echo '?id='.$row['serial']; ?>"><img src="<?php echo $row['picture']?>" height="100" width="120" /></a></div>
<div class="prod_price"><span class="reduce"><?php if($row['rprice'] > 0) {echo "£"; echo $row['rprice'];}?></span> <span class="price"><big style="color:green">£<?php echo $row['price']?></big></span></div>
</div>
<div class="bottom_prod_box"></div>
<div align="center" class="prod_details_tab"> <input type="button" value="Add to Cart" onclick="addtocart(<?php echo $row['serial']?>)" /></td> </div>
</div>
<?php } ?></p>
<?php
}
}
$sql1 = "SELECT * FROM products ";
if(isset($_GET['q'])){
$categories = mysql_real_escape_string($_GET['q']);
$sql .= "WHERE type LIKE '%$categories%'";
$query = mysql_query($sql1) or die(mysql_error());
while ($row = mysql_fetch_assoc($query)) { ?>
<div class="prod_box">
<div class="top_prod_box"></div>
<div class="center_prod_box">
<div class="product_title"><a href="productsview.php<?php echo '?id='.$row['serial']; ?>"><b><?php echo $row['name']?></a></div>
<div class="product_img"><a href="productsview.php<?php echo '?id='.$row['serial']; ?>"><img src="<?php echo $row['picture']?>" height="100" width="120" /></a></div>
<div class="prod_price"><span class="reduce"><?php if($row['rprice'] > 0) {echo "£"; echo $row['rprice'];}?></span> <span class="price"><big style="color:green">£<?php echo $row['price']?></big></span></div>
</div>
<div class="bottom_prod_box"></div>
<div align="center" class="prod_details_tab"> <input type="button" value="Add to Cart" onclick="addtocart(<?php echo $row['serial']?>)" /></td> </div>
</div>
<?php } ?></p>
<?php
}
?>
我不确定这是否符合您的预期,但请试一试。