在日期范围之间选择db行

时间:2014-04-12 16:19:42

标签: php mysql date range

我试图添加一个表单,以便我可以选择一个日期范围,然后重新提交表单,这样我就可以只从表中列出的特定日期范围中获得行,但我不能让它工作得很好。代码什么错了? (php新手警报)(更新)

<html>
<head>
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="HandheldFriendly" content="true">
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> 
<title>EBS Service Skjema</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<link href="print.css" rel="stylesheet" media="print" type="text/css" />
</head>

<body>
<?php
include 'connection.php';
// Check if session is not registered, redirect back to main page. 
// Put this code in first line of web page. 
session_start();
if (!isset($_COOKIE["user"])) {
header("location:login.php");
}
echo "<div class=\"blackbar\">Sjåfør:" .$_COOKIE["user"]."</div>"; 

$dbx = $_POST["databases"];
$con=mysqli_connect("$host", "$username", "$password","$db_name")or die("cannot connect"); 
?>
<div class="bluebox"><form action="data.php" method="post">
<h2 align="center">Utskrift av logger</h2>
<label>Velg type logg du vil skrive ut:</label><br />
<select name="databases">
  <option value="trallevask">Trallevask</option>
  <option value="bilvask">Bilvask</option>
  <option value="maintenance">Vedlikehold</option>
  <option value="diesel">Diesel Stats</option>
  <option value="workhours">Arbeidstid</option>
</select>
<input type="date" name="df" /><input type="date" name="dt" />
<input type="submit" value="Submit"><input type="button" value="Tilbake" onClick="parent.location='index.php'" />
<input type="button" value="Skriv ut" onclick="window.print();return false;" />

</form></div>

<?php
//Row Colors setting
$color1 = "#E1EEf4";
$color2 = "#FFFFFF";
$row_count = 0;

echo "<div class=\"datagrid\"><table width=\"100%\" cellspacing=\"0\" cellpadding=\"5\" border=\"0\">";
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

if ($dbx == "trallevask")   {
    //$result = mysqli_query($con,"SELECT trallevask.regnr, trallevask.date, trallevask.type, equipment.eqname AS vaske_type FROM trallevask JOIN equipment ON equipment.eqid = trallevask.type WHERE userid = ". $_COOKIE['userid']);
    $result = mysqli_query($con, "SELECT trallevask.regnr, trallevask.date, 
trallevask.type, equipment.eqname AS vaske_type 
FROM trallevask JOIN equipment ON equipment.eqid = trallevask.type 
WHERE userid = ". $_COOKIE['userid'] . " AND trallevask.date 
BETWEEN " . $_POST['df'] . " AND " . $_POST['dt']);
    echo "<thead><tr><th>Dato</th><th>Tralle nr.</th><th>Vaskemiddel</th><th>Utført av</th></tr></thead><tbody>";

        while($row = mysqli_fetch_array($result))
        {
            $row_color = ($row_count % 2) ? $color1 : $color2;
            echo "<tr bgcolor=\"$row_color\"><td>" . $row['date'] . "</td><td>" . $row['regnr'] ."</td><td>" . $row['vaske_type'] . "</td><td>".$_COOKIE['user']."</td></tr>";
            $row_count++;
        }
}

elseif ($dbx == "bilvask")  {
    //$result = mysqli_query($con,"SELECT * FROM bilvask, equipment");
    $result = mysqli_query($con,"SELECT bilvask.date, bilvask.type, equipment.eqname AS vaske_type FROM bilvask JOIN equipment ON equipment.eqid = bilvask.type WHERE userid = ". $_COOKIE['userid']);
    echo "<thead><tr><th>Dato</th><th>Beskrivelse</th></tr></thead><tbody>";

        while($row = mysqli_fetch_array($result))
        {
            $row_color = ($row_count % 2) ? $color1 : $color2;
            echo "<tr bgcolor=\"$row_color\"><td>" . $row['date'] . "</td><td>" . $row['vaske_type'] ."</td></tr>";
            $row_count++;
        }

}

elseif ($dbx == "workhours")    {
    $result = mysqli_query($con,"SELECT * FROM workhours WHERE userid = ". $_COOKIE['userid']);
    echo "<thead><tr><th>Dato</th><th>Status</th><th>Klokka</th></tr></thead><tbody>";

        while($row = mysqli_fetch_array($result))
        {
            $row_color = ($row_count % 2) ? $color1 : $color2;      
            echo "<tr bgcolor=\"$row_color\"><td>" . $row['date'] . "</td><td>" . $row['status'] ."</td><td>" . $row['time'] . "</td></tr>";
            $row_count++;       
        }

}

elseif ($dbx == "diesel")   {
    $result = mysqli_query($con,"SELECT * FROM diesel WHERE userid = ". $_COOKIE['userid']);
    echo "<thead><tr><th>Dato</th><th>Km.stand</th><th>Liter</th><th>KR/L</th><th>Stasjon</th><th>Sted</th></tr></thead><tbody>";

        while($row = mysqli_fetch_array($result))
        {
            $row_color = ($row_count % 2) ? $color1 : $color2;      
            echo "<tr bgcolor=\"$row_color\"><td>" . $row['dato'] . "</td><td>" . $row['km'] . "</td><td>" . $row['liter'] . "</td><td>" . $row['krl'] . "</td><td>" . $row['stasjon'] . "</td><td>" . $row['sted'] . "</td></tr>";
            $row_count++;       
        }

}  

elseif ($dbx == "maintenance")  {
    $result = mysqli_query($con,"SELECT * FROM maintenance WHERE userid = ". $_COOKIE['userid']);
    echo "<thead><tr><th>Dato</th><th>Km.stand</th><th>Reg.nummer</th><th>Beskrivelse</th></tr></thead><tbody>";

        while($row = mysqli_fetch_array($result))
        {
            $row_color = ($row_count % 2) ? $color1 : $color2;      
            echo "<tr bgcolor=\"$row_color\"><td>" . $row['date'] . "</td><td>" . $row['mileage'] ."</td><td>" . $row['registration'] ."</td><td>" . $row['info'] ."</td></tr>";
            $row_count++;       
        }

}  

else { 
        echo "Velg en rapport fra menyen over!";
    }

echo "</tbody></table></div>";

mysqli_close($con);
?>
</body>
</html>

3 个答案:

答案 0 :(得分:1)

可能会有所帮助。您还没有在SQL查询中使用df和dt参数:

$result = mysqli_query($con, "SELECT trallevask.regnr, trallevask.date, 
trallevask.type, equipment.eqname AS vaske_type 
FROM trallevask JOIN equipment ON equipment.eqid = trallevask.type 
WHERE userid = ". $_COOKIE['userid'] . " AND trallevask.date 
BETWEEN " . $_POST['df'] . " AND " . $_POST['dt']);

答案 1 :(得分:0)

我首先看到HTML的格式不正确:

更改行:

echo "<div class=\"datagrid\"><table width=\"100%\" cellspacing=\"0\" cellpadding=\"5\"     border=\"0\"><input type=\"submit\" /></form>";

echo "<input type=\"submit\" /></form><div class=\"datagrid\"><table width=\"100%\" cellspacing=\"0\" cellpadding=\"5\" border=\"0\">";

同样在?>添加</table></div>之前的最后一行,因为您需要关闭DIV和TABLE标记。

我也无法看到$_POST["databases"];的来源,这可能会导致代码无法正常工作。

答案 2 :(得分:0)

选择* 从表 其中日期&gt; = [开始日期]和日期&lt; = [结束日期] 或者(我不是100%确定这是否适用于mysql,但它确实适用于postgres!)

选择* 从表 其中[开始日期]和[结束日期]之间的日期