Scusa, errore mio.
Il primo caso che deve essere verificato è quello in cui vengono compilati entrambi i campi
Prova così:
Il primo caso che deve essere verificato è quello in cui vengono compilati entrambi i campi
Prova così:
PHP:
<?php
$DB_SERVER = "";
$DB_USERNAME = "";
$DB_PASSWORD = "";
$DB_DATABASE = "";
$DB = mysqli_connect($DB_SERVER,$DB_USERNAME,$DB_PASSWORD,$DB_DATABASE);
?>
<html>
<head>
<title>Ricerca</title>
<style>
table, td, th, tr {
border: 1px solid #000000;
}
tr, th, td {
padding-left: 7px;
padding-right: 7px;
text-align: center;
}
table {
border-top: 15px;
}
</style>
</head>
<body>
<h1>Ricerca</h1>
<form action="" method="post">
Da: <input name="data1" type="date" value="<?php if (isset($_POST['data1'])) {echo $_POST['data1'];}?>" required/>
a: <input name="data2" type="date" value="<?php if (isset($_POST['data2'])) {echo $_POST['data2'];}?>"/>
<input name="tipouscita" type="text" placeholder="Tipo uscita" value="<?php if (isset($_POST['tipouscita'])) {echo $_POST['tipouscita'];}?>"/>
<input name="matricola" type="text" placeholder="Matricola" value="<?php if (isset($_POST['matricola'])) {echo $_POST['matricola'];}?>"/>
<button name="form-ricerca" type="submit">Cerca</button>
</form>
<?php
if (isset($_POST['form-ricerca'])) {
$nome_tabella = "tblprenotazioneauto";
$field = array("str_data");
if ($_POST['data2']<1) {
$_POST['data2'] = $_POST['data1'];
}
$data1 = $_POST['data1'];
$data1 = explode("-", $data1);
$timestamp1 = mktime(0,0,0,$data1[1],$data1[2],$data1[0]);
$data2 = $_POST['data2'];
$data2 = explode("-", $data2);
$timestamp2 = mktime(23,59,59,$data2[1],$data2[2],$data2[0]);
echo "<table>";
echo "<tr>";
foreach ($field as $thisfield) {
echo "<th>".$thisfield."</th>";
}
echo "</tr>";
if (!empty($_POST['tipouscita']) AND !empty($_POST['matricola'])) {
$query = "SELECT * FROM ".$nome_tabella." WHERE str_data BETWEEN ".$timestamp1." AND ".$timestamp2." AND tipouscita='".$_POST['tipouscita']."' AND matricola='".$_POST['matricola']."'";
}
elseif (!empty($_POST['tipouscita'])) {
$query = "SELECT * FROM ".$nome_tabella." WHERE str_data BETWEEN ".$timestamp1." AND ".$timestamp2." AND tipouscita='".$_POST['tipouscita']."'";
}
elseif (!empty($_POST['matricola'])) {
$query = "SELECT * FROM ".$nome_tabella." WHERE str_data BETWEEN ".$timestamp1." AND ".$timestamp2." AND matricola='".$_POST['matricola']."'";
}
else {
$query = "SELECT * FROM ".$nome_tabella." WHERE str_data BETWEEN ".$timestamp1." AND ".$timestamp2;
}
$result = $DB->query($query);
while ($resultarray=$result->fetch_assoc()) {
echo "<tr>";
foreach ($field as $thisfield) {
if ($thisfield=="str_data") {
echo "<td>".date("d/m/Y",intval($resultarray[$thisfield]))."</td>";
}
else {
echo "<td>".$resultarray[$thisfield]."</td>";
}
}
echo "</tr>";
}
echo "</table>";
}
$DB->close();
?>
</body>
</html>