簡體   English   中英

SQL查詢沒有給出正確的結果

[英]SQL query doesn't give the right result

我有一個表格,人們可以在數據庫中搜索四個值:位置,期間,日期和服務。 我總是沒有得到想要的結果。

如果我使用AND,則人們需要填寫所有內容。 如果我使用OR,則可以獲得完整的數據庫。 我希望能夠在數據庫中搜索那些一到四件事。 有沒有辦法我可以做到這一點?

也許有一種方法可以檢查填寫的字段,並且查詢會隨着填寫的字段自動更改嗎?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"     "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Zoeken</title>

</head>
<body>
<p><a href="new.php"><img src="add.png" width="20px" height="20px"/></a> |   <a href="search.php"><img src="search.png" width="20px" height="20px"/></a> | <a  href="search_lijnen.php"><img src="number.png" width="20px" height="20px"/></a>  </p>
<form action="" method="post">
<div>
<table>
<tr><td><strong>Locatie: </strong></td><td><input type="text" name="Locatie" value="" /></td> </tr>
<tr><td><strong>Periode: </strong></td><td><input type="text" name="Periode" value="" /></td> </tr>
<tr><td><strong>Dag: </strong></td><td><input type="text" name="Dag" value="" /></td> </tr>
<tr><td><strong>Dienst: </strong></td> <td><input type="text" name="Dienst" value="" /></td></tr>
<tr><td></td><td><input type="submit" name="zoeken" value="Zoeken"></td></tr>
</table>

</div>

 </form> 
 <p><a href="new.php"><img src="add.png" width="20px" height="20px"/></a> | <a href="search.php"><img src="search.png" width="20px" height="20px"/></a> | <a href="search_lijnen.php"><img src="number.png" width="20px" height="20px"/></a> </p>
</body>
</html> 
<?php


if (isset($_POST['zoeken']))
 { 
include('connect-db.php');
$Locatie =  $_POST['Locatie'];
$Periode =  $_POST['Periode'];
$Dag =  $_POST['Dag'];
$Dienst =  $_POST['Dienst'];

// get results from database
$result = mysql_query("SELECT * FROM WMC_DeLijn WHERE Locatie='$Locatie' ANY Periode='$Periode' ANY Dag='$Dag'ANY Dienst='$Dienst' ") 
    or die(mysql_error()); 
// display data in table
echo "<h2>Resultaten:</h2><p>";
echo "<table border='1' cellpadding='10'>";
echo "<table><tr><th>ID</th><th>Locatie</th><th>Periode</th><th>Dag</th><th>Dienst</th><th>Delen</th><th>Geleed</th><th>Start 1</th><th>Eind 1</th><th>Start 2</th><th>Eind 2</th><th>Lijnen</th></tr>";


// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {

    // echo out the contents of each row into a table
    echo "<tr>";
    echo '<td align="center">' . $row['id'] . '</td>';
    echo '<td align="center">' . $row['Locatie'] . '</td>';
    echo '<td align="center">' . $row['Periode'] . '</td>';
    echo '<td align="center">' . $row['Dag'] . '</td>';
    echo '<td align="center">' . $row['Dienst'] . '</td>';
    echo '<td align="center">' . $row['Delen'] . '</td>';
    echo '<td align="center">' . $row['Geleed'] . '</td>';
    echo '<td align="center">' . $row['Start1'] . '</td>';
    echo '<td align="center">' . $row['Eind1'] . '</td>';
    echo '<td align="center">' . $row['Start2'] . '</td>';
    echo '<td align="center">' . $row['Eind2'] . '</td>';
    echo '<td align="center">' . $row['Lijnen'] . '</td>';
    //Link to edit record
    echo '<td align="center"><a href="edit.php?id=' . $row['id'] . '"><img src="edit.png" width="20px" height="20px"/></a></td>';
    // Link to delete record
    echo '<td align="center"><a href="delete.php?id=' . $row['id'] . '"><img src="delete.png" width="20px" height="20px"/></a></td>';
     //Link to Add Event to Google Calendar
    echo '<td align="center"><a href="Add_Event.php?id=' . $row['id'] . '"><img src="proceed.png" width="20px" height="20px"/></a></td>';
    echo "</tr>"; 
}}
// close table>
echo "</table>"; 

?>

您可以動態構建查詢字符串,僅在參數不是偽造的情況下添加WHERE子句語句,或者像這樣在SQL本身中添加條件: WHERE (col = ? OR '' = ?) AND (col2 = ? OR '' = ?)

將查詢更改為具有AND條件或OR條件,例如

SELECT * FROM WMC_DeLijn 
WHERE Locatie='$Locatie' 
AND Periode='$Periode' 
AND Dag='$Dag' 
AND ienst='$Dienst';

(要么)

SELECT * FROM WMC_DeLijn 
WHERE Locatie='$Locatie' 
OR Periode='$Periode' 
OR Dag='$Dag' 
OR ienst='$Dienst';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM