【发布时间】:2015-01-11 10:59:59
【问题描述】:
我正在搜索一个网页,该网页应列出不包含所列成分的食谱(用户可以输入一到四种成分,只有第一种是必需的)。
我无法发布图片,所以我会尝试以文字形式显示。
搜索字段(用户可以输入一到四种成分,只有第一种是必需的)
横幅:配方中缺少这些成分。
成分 1 的文本字段(必填):香草
成分 2 的文本字段(可选):牛肉
成分 3 的文本字段(可选):金枪鱼
成分 4 的文本字段(可选):鸭子
搜索按钮
<?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>";
}
?>
现在的情况是,即使我输入了四种成分(见下文),也只考虑输入的第一种成分。
网页中显示的搜索结果(我只显示食谱名称):
食谱名称
烤肋排
金枪鱼红薯外套
培根松子通心粉奶酪
鸭肉汉堡
辣椒酱
羊肉生菜炒
番茄洋葱沙拉
三重奶酪和茄子千层面
煎三文鱼配西洋菜、玉米粥面包丁和刺山柑
燕麦华夫饼
羽衣甘蓝和波多贝罗千层面
在上面,虽然我搜索了不包含香草、牛肉、金枪鱼和鸭的食谱,但我仍然找到了包含牛肉、金枪鱼和鸭的食谱。我在工作台上尝试了同样的事情并收到了成功的结果:
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%';
工作台的结果(好):
配方名称
培根松子通心粉奶酪
羊肉和生菜炒
番茄洋葱沙拉
三重奶酪和茄子千层面
煎三文鱼配西洋菜、玉米粥面包丁和刺山柑
燕麦华夫饼
羽衣甘蓝和波多贝罗千层面
谁能帮忙修改一下php代码?
提前致谢
【问题讨论】:
-
我认为你应该使用 ORM
-
顺便说一句,我是 php 新手,这是一个作业,当前代码有什么解决方法吗?我在网站上有另一个部分列出了列出成分的食谱,而且效果很好。我以为我会复制没有配料的食谱的代码,但这效果不佳。我还尝试颠倒代码,意思是我从 if isset 4 成分开始,以 else if isset 1 成分结束。