php-在数据库上用一个输入进行搜索

kx7yvsdv  于 2021-06-24  发布在  Mysql
关注(0)|答案(2)|浏览(294)

我有3个输入titlu,etaj和descriere,当我只想在titlu上搜索时,不是什么都不显示,而是当我输入所有3个输入时,它的显示。任何建议工作,只有一个输入,但工作与三个输入太多。
代码:

<?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>

关于我的问题,这里有个小问题

bkkx9g8r

bkkx9g8r1#

试试这个:

<?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);
}

?>
w46czmvw

w46czmvw2#

可能像tis:

//......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.....

相关问题