对于我正在创建的系统,我正在尝试生成搜索页面。它有以下HTML:
<?php include 'includes/header.php';
?>
<fieldset>
<legend>Edit all Entries</legend>
<form action="editall.php" method="POST">
<p><label for="mhost">Meeting Host</label><input type="text" name="mhost" id="mhost"></p>
<p><label for="vname">Visitor Name</label><input type="text" name="vname" id="vname"></p>
<p><label for="datepicker">Meeting Date</label><input type="text" name="datepicker" id="datepicker"></p>
<p><input type="submit" name="submit"></p>
</form>
</fieldset>
<?php if(isset($_POST["submit"])==TRUE)
{
$datepicker = trim($_POST["datepicker"]);
if(empty($datepicker)==FALSE)
{
$datepicker = explode("/", $datepicker);
$datepicker = "{$datepicker[2]}-{$datepicker[1]}-{$datepicker[0]}";
}
$mhost = htmlentities(trim($_POST["mhost"]));
$vname = htmlentities(trim($_POST["vname"]));
$db_search = search_database($mhost, $vname, $datepicker, $connection);
if($db_search==FALSE)
{
echo "<div class=\"centered_text\">Nothing found matching search</div>\n";
}
else
{
echo "<table>\n
<thead>\n
<tr>\n
<th>Date</th>\n
<th>Time</th>\n
<th>Visitor(s)</th>\n
<th>Company</th>\n
<th>Host</th>\n
<th>Room</th>\n
<th></th>\n
</tr>\n
</thead>\n
<tbody>\n";
/* This will hold the number of meetings */
foreach($db_search as $meeting)
{
$visitor_names = stripslashes($meeting["visitor_names"]);
$company_name = stripslashes($meeting["company_name"]);
$meeting_host = stripslashes($meeting["meeting_host"]);
$meeting_id = (int)$meeting["meeting_id"];
$start = date("g:ia", strtotime($meeting["meeting_start"]));
$date = date("d/m/y", strtotime($meeting["meeting_start"]));
$meeting_location = stripslashes($meeting["meeting_location"]);
echo "<tr>\n";
echo "<td>{$date}</td>\n";
echo "<td>{$start}</td>\n";
echo "<td>{$visitor_names}</td>\n";
echo "<td>{$company_name}</td>\n";
echo "<td>{$meeting_host}</td>\n";
echo "<td>{$meeting_location}</td>\n";
echo "<td><a href=\"edit.php?id={$meeting_id}\">Edit</a></td>\n";
echo "</tr>\n";
}
echo "</tbody>\n";
echo "</table>\n";
}
}
include 'includes/footer.php';
?>
我查询数据库的函数如下:
function search_database($mhost, $vname, $datepicker, $connection)
{
$mhost = $connection->real_escape_string($mhost);
$mhost = $connection->real_escape_string($vname);
$query_search = $connection->query("SELECT `meeting_id`, `visitor_names`, `meeting_host`, `email_address`, `company_name`, DATE_FORMAT(`meeting_start`, '%Y-%m-%d') AS `meeting_start`, `meeting_location`, `host_extension` FROM `details` WHERE (`meeting_host` LIKE '%{$mhost}%' OR `visitor_names` LIKE '%{$vname}%' OR `meeting_start` = '{$datepicker}') AND (`visibility` = 0) ORDER BY `meeting_start` ASC");
if($connection->error)
{
return $connection->error;
}
if($query_search->num_rows > 0)
{
$results = array();
while($rows = $query_search->fetch_assoc())
{
$results[] = $rows;
}
return $results;
}
else
{
return FALSE;
}
}
问题在于,虽然如果您输入主机名或访客名称,但如果我输入日期,它就无法正常工作。当输入日期时,它将返回所有日期。我不知道问题是什么,请你帮忙
答案 0 :(得分:2)
无法添加评论以寻求澄清(低代表),但MySQL的默认日期格式为YYYY-MM-DD,它不是100%清除您的帖子中的日期您正在格式化正确的格式。如果您使用美国日期,您的MM / DD / YYYY将被转换为YYYY-DD-MM,这将是无效的。
$datepicker = "{$datepicker[2]}-{$datepicker[1]}-{$datepicker[0]}";
因此,如果您使用美国日期,则需要使用:
$datepicker = "{$datepicker[2]}-{$datepicker[0]}-{$datepicker[1]}";