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

报告相同问题?

悬赏问题

  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题