首页 文章

用于从MySQL表中搜索正确值的PHP代码问题

提问于
浏览
0

我有一个网站,里面有搜索功能 . 搜索功能可在bypadmin.ipage.com找到 . 搜索功能运行良好,但有一个小问题 . 当我仅选择 REGION 或仅选择 COUNTRY 或仅选择 DURATION 时,它会显示正确的搜索结果 . 但是当我选择一个县和一个持续时间或一个区域和一个持续时间时,它会显示不正确的结果 .

例如,当我搜索国家:墨尔本和持续时间:搜索中的4天时,它会显示Tour BYP Melbourne,因为它实际上应该显示没有旅游可用,因为旅游在30天内列出 .

类似地,当我选择一个区域和一个持续时间时,它有时会显示未在该区域下列出但在特定持续时间内的游览 . 我想用AND函数搜索,但现在搜索结果显示为OR函数 .

下面是我的PHP代码:

<?php
                 $rf=mysql_query("select * from homepage_place where id=1");
                $fetbtn=mysql_fetch_array($rf);?>
                <div id="left" style="background-color:<?php echo stripslashes($fetbtn['bdy_cl']); ?>">

                 <div id="left_top" style="background-color:<?php echo stripslashes($fetbtn['heading']); ?>">
                 <font style="line-height:45px;"><?php echo stripslashes($fetbtn['heading_text']); ?></font>
                 </div>


                 <form name="searchform" method="post" onSubmit="return Validate();">
                 <table width="80%" style="margin:auto;background:<?php echo stripslashes($fetbtn['bdy_cl']); ?>;
                 ">
                 <tr style="font-size:<?php echo stripslashes($fetbtn['bdy_fntsize']); ?>"><td colspan="2">&nbsp;</td></tr>
                 <tr style="height:30px;">
                 <td height="39"><font style="font-size:<?php echo stripslashes($fetbtn['bdy_fntsize']); ?>;font-style:<?php echo stripslashes($fetbtn['bdy_text']); ?>;color:<?php echo stripslashes($fetbtn['bdy_text_cl']); ?>">Region</font></td>
                 <td>
                 <select name="region" id="region">
                 <?php echo $options; ?>
                                </select>
                 </td>

                 </tr>
                 <tr style="height:30px;">
                 <td height="41"><font style="font-size:<?php echo stripslashes($fetbtn['bdy_fntsize']); ?>;font-style:<?php echo stripslashes($fetbtn['bdy_text']); ?>;color:<?php echo stripslashes($fetbtn['bdy_text_cl']); ?>">Country</font></td>
                 <td>
                 <select name="country" id="country">
                 <option  value="any">All Country</option>
                 <?php
                if($_POST['region'] == "")
 {
    $res_country1 = mysql_query("select DISTINCT country_name from country");
 while($fet1 = mysql_fetch_array($res_country1)){
    $country_name1 = $fet1['country_name'];
    $country_id1 = $fet1['id'];

    echo "<option value='$country_name1'>$country_name1</option>";


 }

    }
                ?>


                 </select>



                 </td>

                 </tr>
                 <tr style="height:30px;">
                 <td height="42"><font style="font-size:<?php echo stripslashes($fetbtn['bdy_fntsize']); ?>;font-style:<?php echo stripslashes($fetbtn['bdy_text']); ?>;color:<?php echo stripslashes($fetbtn['bdy_text_cl']); ?>">Duration</font></td>
                 <td>
                 <select name="duration">
                 <option value="any">
       All Duration
     </option><?php
                $sql_duration=mysql_query("SELECT DISTINCT duration from duration");
                ?>


                 <?php
                 while($fetch_duration=mysql_fetch_array($sql_duration)){?>
                 <option><?php echo $fetch_duration['duration']; ?></option>
                 <?php } ?>



                 </select>

                 </td>


                 </tr>
                 <tr style="height:30px;">
                 <td height="39"><font style="font-size:<?php echo stripslashes($fetbtn['bdy_fntsize']); ?>;font-style:<?php echo stripslashes($fetbtn['bdy_text']); ?>;color:<?php echo stripslashes($fetbtn['bdy_text_cl']); ?>">Keywords</font></td>
                 <td><input style="height:30px;border: 4px solid #ddd;" type="text" name="key" placeholder="Place,activity or code." /></td>
                 </tr>
                 <tr style="height:30px;">
                 <td height="48" colspan="3" align="center">
                 <button style="margin-left:28%;width:130px;height:30px;background-color:<?php echo stripslashes($fetbtn['bellow']); ?>;" type="submit" name="submit">
                 <?php echo stripslashes($fetbtn['button_text']); ?></button>
                 </td>
                 </tr>
                 </table>
                 </form>


                 <?php
                if(isset($_POST["submit"]))
                {
                $region=$_POST["region"];
                $country=$_POST["country"];
                $duration=$_POST["duration"];
                $key=$_POST["key"];

                $search5=mysql_query("select * from tour WHERE region LIKE '%$region%' and duration LIKE '%$duration%'");
                $row5=mysql_num_rows($search5);
                if($row5>0)
                {
                    ?>
                     <script>
 window.location="tours_placewise5.php?region=<?php echo $region; ?>
  & country=<?php echo $country; ?> & duration=<?php echo $duration; ?>"</script>
                     <?php

                    }

                $search=mysql_query("select * from tour WHERE duration LIKE '%$duration%' and country LIKE '%$country%'");
                $row=mysql_num_rows($search);
                if($row>0)
                {
                    ?>
                     <script>
 window.location="tours_placewise.php?region=<?php echo $region; ?>
  & country=<?php echo $country; ?> & duration=<?php echo $duration; ?>"</script>
                     <?php

                    }



 $search1=mysql_query("select * from tour WHERE duration LIKE '%$duration%'");
 $row1=mysql_num_rows($search1);
                if($row1>0)
                {
                    ?>
                     <script>
 window.location="tours_placewise1.php?region=<?php echo $region; ?>
  & country=<?php echo $country; ?> & duration=<?php echo $duration; ?>"</script>
                     <?php

                    }
 $search2=mysql_query("select * from tour WHERE country LIKE '%$country%'");
 $row2=mysql_num_rows($search2);
                if($row2>0)
                {
                    ?>
                     <script>
 window.location="tours_placewise2.php?region=<?php echo $region; ?>
  & country=<?php echo $country; ?> & duration=<?php echo $duration; ?>"</script>
                     <?php

                    }
 $search3=mysql_query("select * from tour WHERE region LIKE '%$region%'");
 $row3=mysql_num_rows($search3);
                if($row3>0)
                {
                    ?>
                     <script>
 window.location="tours_placewise3.php?region=<?php echo $region; ?>
  & country=<?php echo $country; ?> & duration=<?php echo $duration; ?>"</script>
                     <?php

                    }
 $search4=mysql_query("select * from tour WHERE region LIKE '%$region%' and country LIKE '%$country%'");
 $row4=mysql_num_rows($search4);
                if($row4>0)
                {
                    ?>
                     <script>
 window.location="tours_placewise4.php?region=<?php echo $region; ?>
  & country=<?php echo $country; ?> & duration=<?php echo $duration; ?>"</script>
                     <?php

                    }


                    else if($region == any and $country == any and $duration == any)
                    {
                        ?>
                     <script>
                    window.location="tours.php";
                    </script>
                     <?php

                        }


                if($region != "" && $country != "" && $duration != "")
                    {
                        ?><center><span style="color:red;font-size:18px;">Sorry!!! Tours Not Found..</span></center><?php
                        }




                    }
                ?>

这是我的database table

可以询问所需的任何其他信息 .

1 回答

  • 0

    您的所有输入都是选择 . 所以你不需要在你的sql语句中使用LIKE .

    WHERE region LIKE '%$region%'
    

    可以

    WHERE region = '$region'
    

    至于你的持续时间,我建议你不要在数据库中使用varchat类型,而应该使用一个整数来显示游览的天数或小时数(你还可以使用另一个varchar字段输出给用户) . 这将在您的查询中返回更好的结果 .

相关问题