du060334 2017-06-22 08:41
浏览 105

在php mysql中使用多个条件搜索[关闭]

I am trying to search particular data using multiple criteria. I am using age,location, type as multiple search criteria. If the age is 6-12 and type is outdoor only the particular criteria as to be displayed . In the below code it searches if age group is 6-12 and location is oudoor it displays value of age 6-12 and 12-18 whose location is outdoor.It as to be search even if only 1 criteria i.e age or location or type is entered.

Here is the code

<?php
 $username = "root";
$password = "";
$hostname = "localhost"; 
$db = "game";

    // Connect to server and select database.
    $conn=mysql_connect($hostname,$username,$password)or die("cannot connect"); 
    mysql_select_db($db,$conn)or die("cannot select DB");
     if(!$conn)
     {
         die("cannot connect");
     }
if(isset($_POST['submit'])) 
{


$name=$_POST["name"];
$age=$_POST["age"];
$location=$_POST["location"];
$type=$_POST["type"];




 $query = "SELECT * FROM `game1` WHERE age ='$age' or location='$location' or type='$type'";



$run = mysql_query($query);


if(mysql_num_rows($run)>0){
header("Location: gamesearch1.php?age=$age&location=$location&type=$type");
}

else {

    echo "<script>alert('No members Yet!')</script>";
    }
}
?> 
  • 写回答

1条回答 默认 最新

  • douwen2072 2017-06-22 08:49
    关注

    You should use AND instead of OR for your criteria. And you should really use parametrized query!

    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题