douke6027 2014-04-12 16:19
浏览 39
已采纳

在日期范围之间选择db行

Im trying to add a form so I can select a date range then resubmit the form so I can have rows only from a specific date range listed in the table, but I cant get it to work very well. Whats wrong in the code ? (php newbie alert) (UPDATED)

<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条回答 默认 最新

  • dongzhu7329 2014-04-12 16:34
    关注

    May be this will help. You haven't used df and dt parameters in SQL query:

    $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']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥15 怎么实现输入一个要删除的数后删除后显示剩余数再输入再删除显示剩余数(语言-c语言)
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取
  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection