如何使用php-mysqli搜索日期字段
我在搜索mysql数据库的日期字段时遇到问题。。我有一个html表单..允许用户选择3种不同的方式来搜索数据库。。字段1是学生id,字段2是姓氏,字段3是日期。当我运行程序并选择student id时,我会得到正确的结果,当我使用姓氏进行相同操作时,我会得到正确的结果,但当我使用date时,我不会得到任何回报。我碰巧知道结果应该是什么,因为我在数据库中看到了它。除此之外,它的数据记录与学生id和姓氏相同。我想这可能与格式有关,但我想不出来 我不懂aJax,所以现在请不要推荐aJax代码 下面是html代码。 [守则] [/code] 下面是PHP代码 [守则]如何使用php-mysqli搜索日期字段,php,mysql,html,mysqli,Php,Mysql,Html,Mysqli,我在搜索mysql数据库的日期字段时遇到问题。。我有一个html表单..允许用户选择3种不同的方式来搜索数据库。。字段1是学生id,字段2是姓氏,字段3是日期。当我运行程序并选择student id时,我会得到正确的结果,当我使用姓氏进行相同操作时,我会得到正确的结果,但当我使用date时,我不会得到任何回报。我碰巧知道结果应该是什么,因为我在数据库中看到了它。除此之外,它的数据记录与学生id和姓氏相同。我想这可能与格式有关,但我想不出来 我不懂aJax,所以现在请不要推荐aJax代码 下面是h
$records = array();
$typeValue = $_REQUEST['typeValue'];
//If they did not enter a search term we give them an error
if ($typeValue == "")
{
echo "<p>You forgot to enter a search term!!!";
exit;
}
// We perform a bit of filtering
//$typevalue = strtoupper($search);
$typeValue = strip_tags($typeValue);
$typeValue = trim ($typeValue);
$value = $_POST['typeValue'];
if($_POST['type'] == "student_id")
{
//Query with $value on student_id
if($result = $db->query("SELECT * FROM records WHERE student_id LIKE '$typeValue'" )){
if($result->num_rows){
while($row = $result->fetch_object()){
$records[] = $row;
}
$result->free();
}
}
}
elseif($_POST['type'] == "last_name")
{
//Query with $value on last_name
if($result = $db->query("SELECT * FROM records WHERE last_name LIKE '$typeValue'" )){
if($result->num_rows){
while($row = $result->fetch_object()){
$records[] = $row;
}
$result->free();
}
}
}
elseif($_POST['type'] == "examDate")
{
//Query with $value on date
if($result = $db->query("SELECT * FROM records WHERE examDate LIKE '$typeValue'" )){
if($result->num_rows){
while($row = $result->fetch_object()){
$records[] = $row;
}
$result->free();
}
}
}
//This counts the number or results - and if there wasn't any it gives them a little message explaining that
//$anymatches=$result;
//if ($anymatches == 0 )
//{
//echo "Sorry, but we can not find an entry to match your query...<br><br>";
//}
//And we remind them what they searched for
//echo "<b>Results For:</b> " .$typeValue;
//}
?>
<!DOCTYPE html>
<html>
<style type="text/css">
th{text-align: left;}
table, th, td{ border: 1px solid black;}
</style>
<head>
<title>Search Result</title>
</head>
<body>
<h3> Results for <?php echo $typeValue ?> </h3>
<?php
if(!count($records)) {
echo 'No records';
} else {
?>
<table style="width:100%">>
<th>
<tr>
<th>student_id</th>
<th>First name</th>
<th>Last name</th>
<th>email</th>
<th>Major</th>
<th>Exam Name</th>
<th>Taken class</th>
<th>Prepare</th>
<th>MeasureUp Key</th>
<th>Exam Date</th>
<th>Request Made On</th>
</tr>
</thead>
<tbody>
<?php
foreach($records as $r){
?>
<tr>
<td><?php echo $r->student_id; ?></td>
<td><?php echo $r->first_name; ?></td>
<td><?php echo $r->last_name; ?></td>
<td><?php echo $r->email; ?></td>
<td><?php echo $r->major; ?></td>
<td><?php echo $r->examName?></td>
<td><?php echo $r->taken_class; ?></td>
<td><?php echo $r->prepare; ?></td>
<td><?php echo $r->measureUpKey; ?></td>
<td><?php echo $r->examDate; ?></td>
<td><?php echo $r->request_made; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
}
?>
</html>
<html>
<head></head>
<body>
<br/>
<a href="query.html">Return to Search </a>
</body>
</html>
[/code]我相信您正在使用like获取该课程或月份的考试日期,这样您就可以使用它了 使用功能 或者你可能正在寻找一个比
SELECT * FROM records WHERE examDate = '$typeValue'
如何将日期存储在数据库中日期、日期时间、时间戳?停止使用没有通配符的LIKE,也可以只使用=
SELECT * FROM records WHERE DATE_FORMAT(examDate, '%Y %m') = DATE_FORMAT('$typeValue', '%Y %m') ORDER BY examDate
SELECT * FROM records WHERE examDate = '$typeValue'