sql搜索结果太多而且错误

时间:2017-07-24 08:23:28

标签: php mysql select

代码:

<?php
include("config.php");
mysql_connect("Localhost" , "root" , "") or die(mysql_error());
mysql_select_db("imprimante");
if(isset($_POST["submit"])){
  $search = $_POST["search"];
  $sql = mysql_query("SELECT * FROM `inventar` i , `evidenta` e WHERE 
    (i.`ID_M` = '".$search."' and i.`ID_M` = e.`ID_M`) OR (i.`model` LIKE 
    '".$search."') OR (e.`data_intrare` = '".$search."') OR (e.`data_iesire` = 
    '".$search."') OR (i.`user` LIKE '".$search."' and i.`user` = 
    e.`user_iesire`) OR (i.`dept` LIKE '".$search."' and i.`dept` = e.`dept`) OR 
    (i.`tip` LIKE '".$search."') OR (i.`COD` like '".$search."') OR (i.`SERIA` 
    LIKE '".$search."') OR (i.`NR_INV` LIKE '".$search."')");

  if(!$search){
    echo "Nu ai cautat nimic...";
  } else {

  //WHILE PT RESULTURI
  while($row = mysql_fetch_array($sql)){
    echo '<br/> Model: '.$row['model'];
      echo '<br/> ID Model: '.$row['ID_M'];
      echo '<br/> Data Intrare: '.$row['data_intrare'];
      echo '<br/> Data Iesire: '.$row['data_iesire'];
      echo '<br/> User: '.$row['user'];
      echo '<br/> User Iesire: '.$row['user_iesire'];
      echo '<br/> Departament: '.$row['dept'];
      echo '<br/> Tip Consumabil: '.$row['tip'];
      echo '<br/> Cod Produs: '.$row['cod'];
      echo '<br/> Seria: '.$row['SERIA'];
      echo '<br/> Numar Inventar: '.$row['NR_INV'];
      echo '<br/><br/>';
    }
  }
}?>
<html>
  <head><title>Search Function</title></head>
  <body>
    <form action="search.php" method=POST>
      <input type=text name="search" placeholder="Cauta"><br/><br/>
      <input type=submit value="Cauta" name="submit"><br/><br/>
      <button  name="Index"><a href="../index.php">Index</a></button>
      <button  name="Evidenta"><a href="evidenta.php">Evidenta</a></button><br/>
    </form>
  </body>
</html>

为什么此查询返回的结果总是比我在数据库中实际存在的结果多,并且还返回错误的结果?

顺便说一下这两张桌子&#34; inventar&#34;和&#34;明显&#34;通过ID_M

建立关系

感谢您的帮助:D

1 个答案:

答案 0 :(得分:0)

请测试此查询(我认为您的查询中必须管理'或'):

<?php

mysql_query("SELECT * 
             FROM `inventar` i , `evidenta` e 
             WHERE ( i.`ID_M` = '" . $search . "' 
                     AND i.`ID_M` = e.`ID_M`
                   ) 
                   OR ( (i.`model` LIKE '" . $search . "') 
                        OR (e.`data_intrare` = '" . $search . "') 
                        OR (e.`data_iesire` = '" . $search . "') 
                        OR ( i.`user` LIKE '" . $search . "' 
                             AND i.`user` = e.`user_iesire`
                            ) 
                        OR ( i.`dept` LIKE '" . $search . "' 
                             AND i.`dept` = e.`dept`
                           )
                        OR (i.`tip` LIKE '" . $search . "') 
                        OR (i.`COD` like '" . $search . "') 
                        OR (i.`SERIA` LIKE '" . $search . "') 
                        OR (i.`NR_INV` LIKE '" . $search . "')
                   )"
);