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条)

报告相同问题?

悬赏问题

  • ¥60 不懂得怎么运行下载来的代码
  • ¥15 CST导出3D模型图为什么和软件显示不一样?
  • ¥15 加热反应炉PLC控制系统设计(相关搜索:梯形图)
  • ¥15 python 用Dorc包报错,我的写法和网上教的是一样的但是它显示无效参数,是什么问题
  • ¥15 经过滑动平均后的一维信号还原用什么结构好呢?
  • ¥15 指定IP电脑的访问设置
  • ¥30 matlab ode45 未发现警告,但是运行出错
  • ¥15 为什么devc++编译项目会失败啊
  • ¥15 vscode platformio
  • ¥15 代写uni代码,app唤醒