-1

我在 PHP 中开发了表单来搜索 MySQL 数据库,但是无论我如何尝试它仍然无法检索到任何结果。而且我在下面的代码中找不到任何语法错误或其他类似问题,

    <?php
    $txtkv=$_POST['txtkv'];
    $cbgen=$_POST['txtgenerator'];
    $cbinsulation=$_POST['txtinsulation'];
    $cbclass=$_POST['txtclass'];
    $cbairinlet=$_POST['txtairInlet'];
    $cbip=$_POST['txtIp'];

    // set database server access variables: 
    $host = "localhost"; 
    $user = "root"; 
    $pass = ""; 
    $db = "nordhavn";

    $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); 
    mysql_select_db($db) or die ("Unable to select database!"); 

    $query="SELECT Scania.GensetType, Scania.EngineType, Scania.Engine60Hz, Scania.Alternator,NordhavnGenset.MaxKw,  NordhavnGenset.MaxKva, Scania.PriceEur
    FROM Scania 
    LEFT JOIN NordhavnGenset ON Scania.Alternator=NordhavnGenset.Alternator 
    LEFT JOIN Generator ON Generator.Alternator=Scania.Alternator
    LEFT JOIN Insulation ON Insulation.Insulation=NordhavnGenset.Insulation
    LEFT JOIN Klasse ON Klasse.Klasse=NordhavnGenset.Class
    LEFT JOIN AirInletFilter ON AirInletFilter.AirInletFilter=NordhavnGenset.AirInletFilter
    LEFT JOIN IP ON IP.IP=NordhavnGenset.Ip
    WHERE (NordhavnGenset.MaxKva='".$txtkv."')   and (Generator.Alternator='".$cbgen."')  and (Insulation.Insulation='".$cbinsulation."')  and (Klasse.Klasse='".$cbclass."')  and (AirInletFilter.AirInletFilter='".$cbairinlet."')  and (IP.IP='".$cbip."')";        
    $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 
    $i = 0;
    if (mysql_num_rows($result) > 0) { 
          echo "<table cellpadding=10 border=1>
                <tr>
                    <td>    Gense Type (Scania.GensetType)  </td>
                <td>    Engine type (Scania.Enginetype) </td>
                <td>    Engine 60Hz  (Scania.Engine60hz)</td>
                <td>    Alternator (Scania.Alternator and Genrator.Alternator)  </td>   <td>    Max Kw  (NordhavnGenset.MaxKw)  </td>
                    <td>    Max Kva  (NordhavnGenset.MaxKva)</td>
                    <td>    Price Euro (Scania.PriceEur)    </td>
                </tr>";    
          while(($row = mysql_fetch_row($result)) !== false) { 
                $i++;
                echo "<tr class=\"d".($i & 1)."\">"; 
                echo "<td>"."<center>".$row[1]."</center>"."</td>";
                echo "<td>"."<center>".$row[2]."</center>"."</td>";
                echo "<td>"."<center>".$row[3]."</center>"."</td>";
                echo "<td>"."<center>".$row[4]."</center>"."</td>"; 
                echo "<td>"."<center>".$row[5]."</center>"."</td>";
                echo "<td>"."<center>".$row[6]."</center>"."</td>";
                echo "<td>"."<center>".$row[7]."</center>"."</td>";
                echo "<td>"."<center>".$row[8]."</center>"."</td>"; 
                echo "<td>"."<center>".$row[9]."</center>"."</td>";
                echo "<td>"."<center>".$row[10]."</center>"."</td>";
                echo "<td>"."<center>".$row[11]."</center>"."</td>";
                echo "</tr>"; 
            } 
                echo "</table>"; 
        }else { 
            echo "No rows found!"; 
        }     
    mysql_free_result($result); 
    mysql_close($connection); 
    ?>
4

1 回答 1

0

结果你期待什么,你似乎有很多条件。搜索表单应该为用户提供许多标准,但只有在用户提供某些内容时才使用它们。

例如,不要在一个大查询中一次使用所有条件,而是这样做:

$criterias = array("1=1");
if(isset($_POST['txtkv'])){ $criterias[] = 'NordhavnGenset.MaxKva = "'.mysql_real_escape_string($_POST['txtkv']).'"'; }
if(isset($_POST['cbgen'])){ $criterias[] = 'Generator.Alternator = "'.mysql_real_escape_string($_POST['cbgen']).'"'; }
if(isset($_POST['cbinsulation'])){ $criterias[] = 'Insulation.Insulation = "'.mysql_real_escape_string($cbinsulation).'"'; }
if(isset($_POST['cbclass'])){ $criterias[] = 'Klasse.Klasse = "'.mysql_real_escape_string($cbclass).'"'; }
if(isset($_POST['cbairinlet'])){ $criterias[] = 'AirInletFilter.AirInletFilter = "'.mysql_real_escape_string($cbairinlet).'"'; }
if(isset($_POST['cbip'])){ $criterias[] = 'IP.IP = "'.mysql_real_escape_string($cbip).'"'; }

$query = "
SELECT 
    Scania.GensetType, Scania.EngineType, Scania.Engine60Hz, 
    Scania.Alternator,NordhavnGenset.MaxKw, NordhavnGenset.MaxKva, 
    Scania.PriceEur

FROM 
    Scania 
    LEFT JOIN NordhavnGenset ON Scania.Alternator = NordhavnGenset.Alternator 
    LEFT JOIN Generator ON Generator.Alternator = Scania.Alternator
    LEFT JOIN Insulation ON Insulation.Insulation = NordhavnGenset.Insulation
    LEFT JOIN Klasse ON Klasse.Klasse = NordhavnGenset.Class
    LEFT JOIN AirInletFilter ON AirInletFilter.AirInletFilter = NordhavnGenset.AirInletFilter
    LEFT JOIN IP ON IP.IP = NordhavnGenset.Ip

WHERE 
    ".implode(' AND ', $criterias);

$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

这将创建一个仅包含您需要的条件的完整查询。请注意,在顶部,我们创建了标准数组,然后在我们进行时填充所有标准。我们还花时间逃避它们以保护我们在原始请求中没有看到的输入。

然后,使用 IMPLODE,我们将 AND 之间的所有项目整理在一起,以创建标准列表。“1=1”用于防止出现空的 WHERE 子句,以防没有提供标准,但在所有情况下 1=1 始终为真,因此它不会过滤掉任何内容。

我希望这会有所帮助,我希望这真的是你的问题......

祝你好运

于 2012-06-19T15:59:59.623 回答