SQL query doesn't give the right result

user5087002

I have a form where people can search the database for four values: Location, Period, Day and Service. I always do not get the results that I want.

If I use AND, people need to fill in everything. If I use OR I get the complete database. I want to be able to search the database for those one to 4 things. Is there a way how I can do this?

Is there maybe a way to check which fields are filled in, and that the query is automatically changed with the filled in fields?

<!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>"; 

?>
Anonymous

You can either build the query string dynamically, only adding WHERE clause statements if the parameter is not falsy, or add conditions in the SQL itself like so: WHERE (col = ? OR '' = ?) AND (col2 = ? OR '' = ?).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SQL query doesn't give the right result

From Dev

Term Query Doesn't give Proper Result

From Dev

select query doesn't give result in codeigniter?

From Dev

Why laravel query doesn't return right result?

From Dev

Why sql give not right sum in result?

From Dev

SQL query doesn't give any results in PHP

From Dev

Delphi, MS Access, ADO: "a query to a query with parameters" doesn't give proper result

From Dev

Delphi, MS Access, ADO: "a query to a query with parameters" doesn't give proper result

From Dev

Splitting by delimeter doesn't give the right answer

From Dev

Splitting by delimeter doesn't give the right answer

From Dev

Pig: my filter doesn't give result

From Dev

Why OpenMP atomic and critical don't give the right result?

From Dev

Why OpenMP atomic and critical don't give the right result?

From Dev

SPARQL query doesn't give any results

From Dev

Sorting a list doesn't produce the right result

From Dev

My query doesn't return the right count

From Dev

SQL give query result table name without creating new table

From Dev

How to give numbers to the result after executing the query in sql server?

From Dev

std::is_constructible doesn't give the correct result

From Dev

icontains in Django QuerySet doesn't give the case insensitive result?

From Dev

python parsing xml with ElementTree doesn't give interested result

From Dev

Why VS doesn't give me the same result as console in DevTools?

From Dev

A condition in a C program doesn't give the desired result

From Dev

Find command output doesn't give desire result

From Dev

MS Excel PERCENTILE and IF doesn't give correct result

From Dev

Iterating over dataframe doesn't give expected result

From Dev

event.target in function doesn't give expected result

From Dev

MySQL doesn't output PHP query result

From Dev

@Query doesn't return any result

Related Related

  1. 1

    SQL query doesn't give the right result

  2. 2

    Term Query Doesn't give Proper Result

  3. 3

    select query doesn't give result in codeigniter?

  4. 4

    Why laravel query doesn't return right result?

  5. 5

    Why sql give not right sum in result?

  6. 6

    SQL query doesn't give any results in PHP

  7. 7

    Delphi, MS Access, ADO: "a query to a query with parameters" doesn't give proper result

  8. 8

    Delphi, MS Access, ADO: "a query to a query with parameters" doesn't give proper result

  9. 9

    Splitting by delimeter doesn't give the right answer

  10. 10

    Splitting by delimeter doesn't give the right answer

  11. 11

    Pig: my filter doesn't give result

  12. 12

    Why OpenMP atomic and critical don't give the right result?

  13. 13

    Why OpenMP atomic and critical don't give the right result?

  14. 14

    SPARQL query doesn't give any results

  15. 15

    Sorting a list doesn't produce the right result

  16. 16

    My query doesn't return the right count

  17. 17

    SQL give query result table name without creating new table

  18. 18

    How to give numbers to the result after executing the query in sql server?

  19. 19

    std::is_constructible doesn't give the correct result

  20. 20

    icontains in Django QuerySet doesn't give the case insensitive result?

  21. 21

    python parsing xml with ElementTree doesn't give interested result

  22. 22

    Why VS doesn't give me the same result as console in DevTools?

  23. 23

    A condition in a C program doesn't give the desired result

  24. 24

    Find command output doesn't give desire result

  25. 25

    MS Excel PERCENTILE and IF doesn't give correct result

  26. 26

    Iterating over dataframe doesn't give expected result

  27. 27

    event.target in function doesn't give expected result

  28. 28

    MySQL doesn't output PHP query result

  29. 29

    @Query doesn't return any result

HotTag

Archive