【问题标题】:PHP - Search with one input on databasePHP - 在数据库上使用一个输入进行搜索
【发布时间】:2018-12-07 23:27:19
【问题描述】:

我有 3 个输入。 'titlu' 、 'etaj' 和 'descriere' 以及当我想仅在 'titlu' 上搜索时没有显示任何内容,但是当我输入所有 3 个输入时它显示。任何建议仅使用一个输入但也可以使用 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>

Here is a DBFiddle about my question

【问题讨论】:

  • 您的代码容易受到SQL injection attacks 的攻击。您应该通过mysqliPDO 驱动程序使用带有绑定参数的预处理语句。 This post 有一些很好的例子。
  • 对于学校项目来说并不重要。
  • @Alec 最佳实践——比如输入验证、准备好的语句、测试、编码风格等等——很重要。许多人说他们“只是希望它工作”并声称他们会“稍后修复它”。尽管他们甚至可能是真诚的,但经验表明这几乎从未发生过。不要成为那种人!第一次做对!
  • 两件事可以帮助您解决问题。 (1) 设置好sql语句后添加echo $sql,这样就可以看到你正在使用的准确的sql字符串。 (2) 用 try/catch 包装你的 sql 操作,这样你就可以看到返回了什么错误,如果有的话。此外,正如@Luca 所建议的那样,更好、更可靠的方法是使用绑定参数,并且它需要额外的 1 分钟来实现,所以一旦你采取了,真的没有任何好的理由不这样做几分钟了解如何操作。

标签: javascript php jquery mysql search


【解决方案1】:

试试这个:

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


?>

【讨论】:

    【解决方案2】:

    也许是这样:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-28
      • 2017-11-08
      相关资源
      最近更新 更多