gpt4 book ai didi

PHP 和 MySQL 通过 GET 循环相同的查询字符串

转载 作者:行者123 更新时间:2023-11-29 14:15:45 24 4
gpt4 key购买 nike

我正在尝试在类别页面上进行电子商务过滤。到目前为止,我可以让它适用于一种颜色,但如果用户检查多种颜色,我希望它也搜索另一种颜色。

网址

//current
category.php?colourChoice=White
//more than one
category.php?colourChoice=White&colourChoice=Ruby+Red

我意识到我需要某种形式的数组或爆炸?

MYSQL

products
---
id sku price
1 p22 45.00
2 p33 65.00
3 p44 70.00

colours
---
id sku name
1 p22 White
2 p33 Ruby Red
3 p44 Black

HTML

<form action="form.php" method="get">
<input type="checkbox" name="colourChoice" value="White"/>
<input type="checkbox" name="colourChoice" value="Black"/>
<input type="checkbox" name="colourChoice" value="Ruby Red"/>
<input type="checkbox" name="colourChoice" value="Orange"/>
<input type="submit" value="go">
</form>

PHP

// VARS
$colourName=mysql_real_escape_string($_GET["colourChoice"]);

// MYSQL
$checkSQL=mysql_query("
SELECT * FROM `products`
INNER JOIN `colours`
ON `products`.`sku` = `colours`.`sku`
WHERE (
name = '".$colourName."'
OR name LIKE '".$colourName.";%'
OR name LIKE '%;".$colourName.";%'
OR name LIKE '%;".$colourName."'
)
");

// SHOW RESULTS
while($r = mysql_fetch_array($checkSQL)) {
echo '
<div class="product">
Cost £'.$r['price'].'<br />
</div>
';
}

最佳答案

您必须按如下方式制作复选框数组

<form action="form.php" method="get">
<input type="checkbox" name="colourChoice[]" value="White"/>
<input type="checkbox" name="colourChoice[]" value="Black"/>
<input type="checkbox" name="colourChoice[]" value="Ruby Red"/>
<input type="checkbox" name="colourChoice[]" value="Orange"/>
<input type="submit" value="go">
</form>

然后,您将在 php 中将多个值 checkin 数组。

foreach($_GET["colourChoice"] as $value){
/// here you can make concat for where clause.
}

关于PHP 和 MySQL 通过 GET 循环相同的查询字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12723359/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com