【问题标题】:problems with if isset phpif isset php的问题
【发布时间】: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 成分结束。

标签: php mysql


【解决方案1】:

问题在于检查顺序,假设您获得了所有 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
}

【讨论】:

  • 当我尝试这个时,没有返回任何结果。我认为这是因为文本字段发送的是空白值,这就是表单发送的内容:?ingredient1=vanilla&ingredient2=&ingredient3=&ingredient4=
  • 我无法访问您的 localhost,抱歉)))
  • 然后检查isset而不是empty
  • 编辑了链接以仅显示表单发送的内容
  • 仍然是相同的解决方案 - 检查成分是否 isset() 并且它的值不是 empty()
【解决方案2】:

您应该使用 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%';

【讨论】:

    猜你喜欢
    • 2017-04-10
    • 1970-01-01
    • 2015-10-14
    • 2017-12-16
    • 1970-01-01
    • 2020-10-25
    • 1970-01-01
    • 1970-01-01
    • 2017-10-25
    相关资源
    最近更新 更多