我正在构建一个网页搜索,该网页应列出不包含所列成分的食谱(用户可以输入一到四种成分,只需要第一种成分)。
我无法发布图片,因此我会尝试以文字显示。
搜索字段(用户可以输入一到四种成分,只需要第一种成分)
横幅:食谱缺少这些成分。
成分1的文本字段(必填):香草
成分2的文本字段(可选):牛肉
成分3的文本字段(可选):tuna
成分4的文本字段(可选):duck
搜索按钮
<?php
//CONNECT TO DATABASE
include_once ("dbc.php");
//DECLAIR SESSION USERNAME
$username=$_SESSION['username'];
$getMemberID = mysql_query ("select member_id from members where member_username = '$username'");
$memberID = mysql_result($getMemberID,0);
//GET RECIPES WITHOUT INGREDIENT
if (isset($_GET['ingredient1'])) {
$ingredient1=$_GET['ingredient1'];
//GET RECIPES FROM DATABASE
$getAllRecipes = mysql_query ("select recipes.recipe_id, recipes.member_id, recipes.recipe_name as 'recipeName', cooking_time.cooking_time as 'cookingTime', concat(member_name, ' ', member_surname) as 'author' from members left join recipes ON members.member_id = recipes.member_id left join cooking_time ON recipes.cooking_time_id = cooking_time.cooking_time_id where recipes.recipe_ingredients not like '%$ingredient1%'");
} else if (isset($_GET['ingredient1'], $_GET['ingredient2'])){
$ingredient1=$_GET['ingredient1'];
$ingredient2=$_GET['ingredient2'];
//GET RECIPES FROM DATABASE
$getAllRecipes = mysql_query ("select recipes.recipe_id, recipes.member_id, recipes.recipe_name as 'recipeName', cooking_time.cooking_time as 'cookingTime', concat(member_name, ' ', member_surname) as 'author' from members left join recipes ON members.member_id = recipes.member_id left join cooking_time ON recipes.cooking_time_id = cooking_time.cooking_time_id where recipes.recipe_ingredients not like '%$ingredient1%' and recipes.recipe_ingredients not like '%$ingredient2%'");
} else if (isset($_GET['ingredient1'], $_GET['ingredient2'], $_GET['ingredient3'])) {
$ingredient1=$_GET['ingredient1'];
$ingredient2=$_GET['ingredient2'];
$ingredient3=$_GET['ingredient3'];
//GET RECIPES FROM DATABASE
$getAllRecipes = mysql_query ("select recipes.recipe_id, recipes.member_id, recipes.recipe_name as 'recipeName', cooking_time.cooking_time as 'cookingTime', concat(member_name, ' ', member_surname) as 'author' from members left join recipes ON members.member_id = recipes.member_id left join cooking_time ON recipes.cooking_time_id = cooking_time.cooking_time_id where recipes.recipe_ingredients not like '%$ingredient1%' and recipes.recipe_ingredients not like '%$ingredient2%' and recipes.recipe_ingredients not like '%$ingredient3%'");
} else if (isset($_GET['ingredient1'], $_GET['ingredient2'], $_GET['ingredient3'], $_GET['ingredient4'])) {
$ingredient1=$_GET['ingredient1'];
$ingredient2=$_GET['ingredient2'];
$ingredient3=$_GET['ingredient3'];
$ingredient4=$_GET['ingredient4'];
//GET RECIPES FROM DATABASE
$getAllRecipes = mysql_query ("select recipes.recipe_id, recipes.member_id, recipes.recipe_name as 'recipeName', cooking_time.cooking_time as 'cookingTime', concat(member_name, ' ', member_surname) as 'author' from members left join recipes ON members.member_id = recipes.member_id left join cooking_time ON recipes.cooking_time_id = cooking_time.cooking_time_id where recipes.recipe_ingredients not like '%$ingredient1%' and recipes.recipe_ingredients not like '%$ingredient2%' and recipes.recipe_ingredients not like '%$ingredient3%' and recipes.recipe_ingredients not like '%$ingredient4%'");
}
//POPULATE TABLE ROWS WITH DATA FROM DATABASE
while ($allRecipes = mysql_fetch_array ($getAllRecipes)) {
//GET USERNAME TO USE FOR ENABLING MODIFY/DELETE
$dbMemberID = $allRecipes['member_id'];
$getDbUsername = mysql_query ("select members.member_username from members where members.member_id = '$dbMemberID'");
$dbUsername = mysql_result($getDbUsername,0);
//CREATING TABLE ROWS WITH RECIPE INFORMATION
echo "<tr>";
echo "<td><a href='show_recipe.php?id=" . $allRecipes['recipe_id'] . "'>" . $allRecipes['recipeName'] . "<a></td>";
echo "<td>" . $allRecipes['cookingTime'] . "</td>";
echo "<td>" . $allRecipes['author'] . "</td>";
if ($username === $dbUsername) {
echo "<td align='center'><a href='modify_recipe.php?id=" . $allRecipes['recipe_id'] . "'>Modify<a></td>";
echo "<td align='center'><a href='delete_recipe.php?id=" . $allRecipes['recipe_id'] . "'>Delete<a></td>";
} else {
echo "<td></td>";
echo "<td></td>";
}
echo "</tr>";
}
?>
现在的方式,即使我输入了四种成分(见下文),也只考虑输入的第一种成分。
网页中显示的搜索结果(我只显示食谱名称):
食谱名称
Rib Roast
金枪鱼红薯夹克
通心粉奶酪配培根&amp;松子
Duck confit burger
Chilli con carne
Lamb&amp;生菜煎炸
番茄&amp;洋葱沙拉
三重奶酪&amp;茄子烤宽面条
煎三文鱼配豆瓣菜,玉米粥油炸面包块&amp;刺山柑
燕麦华夫饼干
Kale和Portobello Lasagna
在上面,虽然我搜索不含香草,牛肉,金枪鱼和鸭肉的食谱,但我仍然得到含有牛肉,金枪鱼和鸭肉的食谱。我在工作台上尝试了同样的事情并获得了成功的结果:
select
recipes.recipe_id,
recipes.member_id,
recipes.recipe_name as 'recipeName',
cooking_time.cooking_time as 'cookingTime',
concat(member_name, ' ', member_surname) as 'author'
from
members
left join
recipes ON members.member_id = recipes.member_id
left join
cooking_time ON recipes.cooking_time_id = cooking_time.cooking_time_id
where
recipes.recipe_ingredients not like '%Vanilla%'
and recipes.recipe_ingredients not like '%beef%'
and recipes.recipe_ingredients not like '%tuna%'
and recipes.recipe_ingredients not like '%duck%';
工作台的结果(好):
RecipeName中
通心粉奶酪配培根&amp;松子
Lamb&amp;生菜煎炸
番茄&amp;洋葱沙拉
三重奶酪&amp;茄子烤宽面条
煎三文鱼配豆瓣菜,玉米粥油炸面包块&amp;酸豆
燕麦华夫饼干
Kale和Portobello Lasagna
任何人都可以帮助修改php代码吗?
提前致谢
答案 0 :(得分:0)
你应该使用OR代替AND,因为它现在只会查找没有所有给定项目(香草,牛肉,金枪鱼,鸭子)的东西,只有一个,两个或其中三个会出现在你的搜索中。
请改为尝试:
select
recipes.recipe_id,
recipes.member_id,
recipes.recipe_name as 'recipeName',
cooking_time.cooking_time as 'cookingTime',
concat(member_name, ' ', member_surname) as 'author'
from
members
left join
recipes ON members.member_id = recipes.member_id
left join
cooking_time ON recipes.cooking_time_id = cooking_time.cooking_time_id
where
recipes.recipe_ingredients not like '%Vanilla%'
OR recipes.recipe_ingredients not like '%beef%'
OR recipes.recipe_ingredients not like '%tuna%'
OR recipes.recipe_ingredients not like '%duck%';
答案 1 :(得分:0)
问题是检查的顺序,让我们说你得到所有4种成分。
首先检查isset($_GET['ingredient1'])
设置了 Ingedient1 ?当然!然后你的sql-query生成并执行。没有进一步的检查。
您应该将if
语句从所有成分重建为一个:
if (isset($_GET['i1'], $_GET['i2'], $_GET['i3'], $_GET['i4'])) {
// do something
} else if (isset($_GET['i1'], $_GET['i2'], $_GET['i3'])) {
// do something
} else if (isset($_GET['i1'], $_GET['i2'])) {
// do something
} else if (isset($_GET['i1'])) {
// do something
}