首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >PHP -在数据库上只需输入一次即可进行搜索

PHP -在数据库上只需输入一次即可进行搜索
EN

Stack Overflow用户
提问于 2018-06-29 03:53:09
回答 2查看 72关注 0票数 1

我有3个输入。'titlu‘,'etaj’和'descriere‘,当我只想在'titlu’上搜索时,并不是什么都没有显示,而是当我输入所有3个输入时,它就会显示出来。任何建议的工作,只有一个输入,但工作与3个输入。

代码:

代码语言:javascript
复制
<?php
$con = mysqli_connect("localhost","rent","123");
if (!$con)
{
die('Could not connect: ' . mysqli_error());
}
mysqli_select_db($con, "rent") or die("ERROR");


if(isset($_REQUEST['submit'])){
    $titlu=$_POST['titlu'];
    $etaj=$_POST['etaj'];
	$descriere=$_POST['descriere'];
    $sql=" SELECT * FROM apartament WHERE titlu like '%".$titlu."%' OR etaj like '%".$etaj."%' OR descriere like '%".$descriere."%'";
    $q=mysqli_query($con, $sql);
}
else{
    $sql="SELECT * FROM apartament";
    $q=mysqli_query($con, $sql);
}
?>
<form method="post">
    <table width="200" border="1">
  <tr>
    <td>Titlu</td>
    <td><input type="text" name="titlu" value="<?php echo $titlu;?>" /></td>
    <td>Etaj</td>
    <td><input type="text" name="etaj" value="<?php echo $etaj;?>" /></td>
	    <td><input type="text" name="descriere" value="<?php echo $descriere;?>" /></td>
    <td><input type="submit" name="submit" value=" Find " /></td>
  </tr>
</table>
</form>
<table>
    <tr>
        <td>Titlu</td>
        <td>Etaj</td>
    </tr>
    <?php
    while($res=mysqli_fetch_array($q)){
    ?>
    <tr>
        <td><?php echo $res['titlu'];?></td>
        <td><?php echo $res['etaj'];?></td>
		<td><?php echo $res['descriere'];?></td>
    </tr>
    <?php }?>
</table>

Here is a DBFiddle about my question

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-29 04:48:52

试试这个:

代码语言:javascript
复制
<?php

$con = mysqli_connect("localhost","rent","123");
if (!$con)
{
die('Could not connect: ' . mysqli_error());
}
mysqli_select_db($con, "rent") or die("ERROR");


    $titlu = ($_POST['titlu'] && $_POST['titlu'] != "") ? $_POST['titlu'] : "";
    $etaj = ($_POST['etaj'] && $_POST['etaj'] != "") ? $_POST['etaj'] : "";
    $descriere = ($_POST['descriere'] && $_POST['descriere'] != "") ? $_POST['descriere'] : "";
    $sql = " SELECT * FROM apartament";
    $sql .= ($titlu != "" or $etaj != "" or $descriere != "") ? " WHERE " : " ";
    $sql .= ($titlu != "") ? " titlu like '%".$titlu."%'" : "";
    $sql .= ($titlu != "" and $etaj != "") ? " OR " : "";
    $sql .= ($etaj != "") ? " etaj like '%".$etaj."%' " : "";
    $sql .= (($titlu != "" or $etaj != "") and $descriere != "") ? " OR " : "";
    $sql .= ($descriere != "") ? " descriere like '%".$descriere."%'" : "";
    $sql .= ";";
    $q=mysqli_query($con, $sql);
}


?>
票数 0
EN

Stack Overflow用户

发布于 2018-06-29 04:38:27

也许就像下面这样:

代码语言:javascript
复制
    //......CUT......
 if(isset($_REQUEST['submit'])){
   $where_str='';
   if(isset($_POST['titlu']) AND $_POST['titlu']!=''){
      $where_str.="titlu like '%".$_POST['titlu']."%'";
   }
   if(isset($_POST['etaj']) AND $_POST['etaj']!=''){
      if($where_str!=""){$where_str.=" OR ";}
      $where_str.="etaj like '%".$_POST['etaj']."%'";
   }
   if(isset($_POST['descriere']) AND $_POST['descriere']!=''){
      if($where_str!=""){$where_str.=" OR ";}
      $where_str.="descriere like '%".$_POST['descriere']."%'";
   }

       $sql=" SELECT * FROM apartament WHERE ".$where_str;
       $q=mysqli_query($con, $sql);
 }
    //.......CUT.....
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51090359

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档