我为一个表创建了一个过滤器,一开始工作得很好,但是一旦我用datepicker替换了日期的输入形式,我注意到只有涉及日期的过滤器才有效,而另一个被忽略了我无法理解为什么它会像这样。
这是为过滤器选项创建下拉列表的部分:
echo "<form action='listTimes.php' method='post'>
<table>
<caption><b>Filter By:</b></caption>
<tr>
<th>Task Name</th>
<th>Task Stage</th>
<th>Name</th>
<th>Date</th>
</tr>
<tr>
<td>";
// first filter option - dropdown
$sql = "SELECT DISTINCT taskName FROM tasks";
$result = mysqli_query($link, $sql);
echo "<select name='taskName'>";
echo "<option selected='selected' value='' disabled='disabled'></option>";
while ($row = mysqli_fetch_array($result)) {
if (isset($row['taskName'])) {
echo "<option value='" . $row['taskName'] . "'>" . $row['taskName'] . "</option>";
}
}
echo "</select>
</td>
<td>";
// second filter option - dropdown
$sql = "SELECT DISTINCT taskStage FROM taskattributes";
$result = mysqli_query($link, $sql);
echo "<select name='taskStage'>
<option selected='selected' value='' disabled='disabled'></option>";
while ($row = mysqli_fetch_array($result)) {
if (isset($row['taskStage'])) {
echo "<option value='" . $row['taskStage'] . "'>" . $row['taskStage'] . "</option>";
}
}
echo "</select>
</td>
<td>";
// third filter option - dropdown
$sql = "SELECT DISTINCT userName FROM users";
$result = mysqli_query($link, $sql);
echo "<select name='userName'>";
echo "<option selected='selected' value='' disabled='disabled'></option>";
while ($row = mysqli_fetch_array($result)) {
if (isset($row['userName'])) {
echo "<option value='" . $row['userName'] . "'>" . $row['userName'] . "</option>";
}
}
/* fourth filter option, was just a simple text input at first. Now I changed it to a datepicker, which works, but the other three filter options stopped working at this point */
echo "</select>
</td>
<td>
<input type='text' class='datepicker' name='entryDate'>
</td>
</tr>
</table>
<div align='center'> <input type='submit' value='Filter'></div>
</form>
后面的过滤机制写得非常糟糕,但效果很好:
if(!isset($_POST['taskName']) && !isset($_POST['taskStage']) && !isset($_POST['userName']) && !isset($_POST['entryDate']))
{
$sql = "SELECT * FROM timeEntry";
$result = $link->query($sql);
}
elseif (isset($_POST['taskName']) && !isset($_POST['taskStage']) && !isset($_POST['userName']) && !isset($_POST['entryDate']))
{
$taskName = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskName']));
$sql = "SELECT * FROM timeEntry WHERE taskName='$taskName'";
$result = $link->query($sql);
}
elseif (isset($_POST['taskName']) && isset($_POST['taskStage']) && !isset($_POST['userName']) && !isset($_POST['entryDate']))
{
$taskName = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskName']));
$taskStage = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskStage']));
$sql = "SELECT * FROM timeEntry WHERE taskName='$taskName' AND taskStage='$taskStage'";
$result = $link->query($sql);
}
elseif (isset($_POST['taskName']) && isset($_POST['taskStage']) && !isset($_POST['userName']) && !isset($_POST['entryDate']))
{
$taskName = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskName']));
$taskStage = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskStage']));
$userName = mysqli_real_escape_string($link, htmlspecialchars($_POST['userName']));
$sql = "SELECT * FROM timeEntry WHERE taskName='$taskName' AND taskStage='$taskStage' AND userName='$userName'";
$result = $link->query($sql);
}
elseif (isset($_POST['taskName']) && isset($_POST['taskStage']) && isset($_POST['userName']) && isset($_POST['entryDate']))
{
$taskName = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskName']));
$taskStage = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskStage']));
$userName = mysqli_real_escape_string($link, htmlspecialchars($_POST['userName']));
$entryDate = mysqli_real_escape_string($link, htmlspecialchars($_POST['entryDate']));
$sql = "SELECT * FROM timeEntry WHERE taskName='$taskName' AND taskStage='$taskStage' AND userName='$userName' AND entryDate='$entryDate'";
$result = $link->query($sql);
}
elseif (!isset($_POST['taskName']) && isset($_POST['taskStage']) && isset($_POST['userName']) && isset($_POST['entryDate']))
{
$taskStage = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskStage']));
$userName = mysqli_real_escape_string($link, htmlspecialchars($_POST['userName']));
$entryDate = mysqli_real_escape_string($link, htmlspecialchars($_POST['entryDate']));
$sql = "SELECT * FROM timeEntry WHERE taskStage='$taskStage' AND userName='$userName' AND entryDate='$entryDate'";
$result = $link->query($sql);
}
elseif (!isset($_POST['taskName']) && !isset($_POST['taskStage']) && isset($_POST['userName']) && isset($_POST['entryDate']))
{
$userName = mysqli_real_escape_string($link, htmlspecialchars($_POST['userName']));
$entryDate = mysqli_real_escape_string($link, htmlspecialchars($_POST['entryDate']));
$sql = "SELECT * FROM timeEntry WHERE userName='$userName' AND entryDate='$entryDate'";
$result = $link->query($sql);
}
elseif (!isset($_POST['taskName']) && !isset($_POST['taskStage']) && !isset($_POST['userName']) && isset($_POST['entryDate']))
{
$entryDate = mysqli_real_escape_string($link, htmlspecialchars($_POST['entryDate']));
$sql = "SELECT * FROM timeEntry WHERE entryDate='$entryDate'";
$result = $link->query($sql);
}
elseif (!isset($_POST['taskName']) && isset($_POST['taskStage']) && !isset($_POST['userName']) && isset($_POST['entryDate']))
{
$taskStage = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskStage']));
$entryDate = mysqli_real_escape_string($link, htmlspecialchars($_POST['entryDate']));
$sql = "SELECT * FROM timeEntry WHERE taskStage='$taskStage'AND entryDate='$entryDate'";
$result = $link->query($sql);
}
elseif (isset($_POST['taskName']) && !isset($_POST['taskStage']) && isset($_POST['userName']) && !isset($_POST['entryDate']))
{
$taskName = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskName']));
$userName = mysqli_real_escape_string($link, htmlspecialchars($_POST['userName']));
$sql = "SELECT * FROM timeEntry WHERE taskName='$taskName' AND userName='$userName'";
$result = $link->query($sql);
}
elseif (isset($_POST['taskName']) && !isset($_POST['taskStage']) && isset($_POST['userName']) && isset($_POST['entryDate']))
{
$taskName = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskName']));
$userName = mysqli_real_escape_string($link, htmlspecialchars($_POST['userName']));
$entryDate = mysqli_real_escape_string($link, htmlspecialchars($_POST['entryDate']));
$sql = "SELECT * FROM timeEntry WHERE taskName='$taskName' AND userName='$userName' AND entryDate='$entryDate'";
$result = $link->query($sql);
}
elseif (!isset($_POST['taskName']) && isset($_POST['taskStage']) && !isset($_POST['userName']) && !isset($_POST['entryDate']))
{
$taskStage = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskStage']));
$sql = "SELECT * FROM timeEntry WHERE taskStage='$taskStage'";
$result = $link->query($sql);
}
elseif (isset($_POST['taskName']) && isset($_POST['taskStage']) && !isset($_POST['userName']) && isset($_POST['entryDate']))
{
$taskName = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskName']));
$taskStage = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskStage']));
$entryDate = mysqli_real_escape_string($link, htmlspecialchars($_POST['entryDate']));
$sql = "SELECT * FROM timeEntry WHERE taskName='$taskName' AND taskStage='$taskStage' AND entryDate='$entryDate'";
$result = $link->query($sql);
}
elseif (!isset($_POST['taskName']) && !isset($_POST['taskStage']) && isset($_POST['userName']) && !isset($_POST['entryDate']))
{
$userName = mysqli_real_escape_string($link, htmlspecialchars($_POST['userName']));
$sql = "SELECT * FROM timeEntry WHERE userName='$userName'";
$result = $link->query($sql);
}
elseif (isset($_POST['taskName']) && !isset($_POST['taskStage']) && !isset($_POST['userName']) && isset($_POST['entryDate']))
{
$taskName = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskName']));
$entryDate = mysqli_real_escape_string($link, htmlspecialchars($_POST['entryDate']));
$sql = "SELECT * FROM timeEntry WHERE taskName='$taskName' AND entryDate='$entryDate'";
$result = $link->query($sql);
}
elseif (!isset($_POST['taskName']) && isset($_POST['taskStage']) && isset($_POST['userName']) && !isset($_POST['entryDate']))
{
$taskStage = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskStage']));
$userName = mysqli_real_escape_string($link, htmlspecialchars($_POST['userName']));
$sql = "SELECT * FROM timeEntry WHERE taskStage='$taskStage' AND userName='$userName'";
$result = $link->query($sql);
}
echo "<table class='top1' border='1'>
<tr>
<th>Entry No.</th>
<th>Task/Activity</th>
<th>Task Stage</th>
<th>User Name</th>
<th>Time Spent</th>
<th>Date</th>
<th>Edit Entry</th>
<th>Delete Entry</th>
</tr>";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>
<td align='center'>" . $row['timeId'] . "
</td>";
echo "<td>" . $row['taskName'] . "</td>";
echo "<td>" . $row['taskStage'] . "</td>";
echo "<td>" . $row['userName'] . "</td>";
echo "<td align='center'>" . $row['timeSpent'] . "</td>";
$originaldate = mysqli_real_escape_string($link, $row['entryDate']);
$newdate = date("d/m/Y", strtotime($row['entryDate']));
echo "<td align='center'>" . $newdate . "</td>";
echo "<td align='center'><a href='editAllTimes.php?timeId=" . $row["timeId"] . "'>Edit</a></td>";
echo "<td align='center'><a href='deleteAllTime.php?timeId=" . $row["timeId"] . "' onclick='return checkDelete()'>Delete</a></td>
</tr>";
}
echo "</table>";
}
这段代码看起来很糟糕,在添加日期选择器之前工作得很好:
<input type='text' class='datepicker' name='entryDate'>
我注意到一种奇怪的行为:
这种行为有什么理由吗?和任何解决方案?
答案 0 :(得分:2)
我认为<input>
元素的值始终显示在$ _POST中,尽管已填充或未填充。你应该在字段上尝试isset()和empty()检查。