php - 如何使用 php - mysqli 搜索日期字段

标签 php mysql html mysqli

我在搜索mysql数据库的日期字段时遇到问题..我有一个html表单..允许用户选择3种不同的方式来搜索数据库..字段1是student_id,字段2是姓氏,字段3 是日期。好吧,当我运行程序并选择学生 ID 时,我得到了正确的结果,当我使用姓氏执行相同操作时,我得到了正确的结果,但是当我使用日期时......我没有得到任何返回。我碰巧知道结果应该是什么,因为我在数据库中看到它......除此之外,它的数据记录与学生 ID 和姓氏相同。我认为这可能与格式有关,但我无法弄清楚..

我不了解 aJax,所以现在请不要建议 ajax 代码。

这是 html 代码。 [代码]

-- start javascript -->
<script type="text/javascript">
/*<![CDATA[ */
   function check(){
    if(document.lastname.last.value == "" || document.lastname.last.value == null)
    {
        alert("no last name entered");
        return false;
    }
}
function checkdate() {
var date_regex = /^(0[1-9]|1[0-2])\/(0[1-9]|1\d|2\d|3[01])\/(19|20)\d{2}$/ ;
if(!(date_regex.test(testDate)))
{
return false;
}

}

function fieldSwap(image){
 var sb = document.getElementById('sb');
 if(sb.value == ""){
  sb.style.background = "url(images/"+image+") no-repeat";
  }
}

function buttonSwap(image){
 var sb = document.getElementById('sb');
 sb.src = "images/"+image;
}
function validate(){
var x = document.information.search.value;
if (x.length<10 ||  x.length>10){
alert("student id is incorrect");
return false;
}
}

/*]]> */    
</script>
<!-- end javascript -->




</head>


<body>

<div id="form_wrap"><!-- start form wrap -->
     <div id="form_header">
     </div>
     <div id="form_body">
     <p>Search for a certification request (Enter one of the following):</p>
     <form action="search.php" method="POST" name="information" id="information" onsubmit="return(validate()or return(checkdate())">

       <div class="field">  
            <select name="type">
                <option value="student_id">Student ID</option>
                <option value="last_name">Last name</option>
                <option value="examDate">Exam date</option>
            </select>
            <input name="typeValue" value="" />
        <input type="submit" value="Search" />
     </form>
    </div>
</div>  

    </div><!-- end form wrap -->

    </body>
    </html>

<form action = "" method = "POST">
            <div class="field">
                <label for = "first_name"> first_name</label>
                <input type = "text" name = "first_name" id = "first_name">
            </div>
            <div class = "field">
                <label for = "last_name"> last_name </label>
                <input type ="text" name = "last_name" id = "last_name">
            </div>
            <div class = "field">
                <label for = "bio"> bio </label>
                <textarea name = "bio" id = "bio"></textarea>   
            </div>
            <input type = "submit" value = "Insert">

        </form>

[/代码]

这是 PHP 代码

[代码]

$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>

[/代码]

最佳答案

我相信您正在使用类似的方法来获取该 session 或月份的考试日期,以便您可以使用它 使用DATE_FORMAT功能

SELECT * FROM records WHERE DATE_FORMAT(examDate, '%Y %m') = DATE_FORMAT('$typeValue', '%Y %m') ORDER BY examDate

或者您可能正在寻找特定日期

SELECT * FROM records WHERE examDate = '$typeValue' 

关于php - 如何使用 php - mysqli 搜索日期字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25965127/

相关文章:

python - 创建一个新的 sql 表,其中的列源自另一个 sql 表

html - Mac Safari 上的 CSS 列表项额外填充

php - 如何检查 PHP 中的前 2 个字符(不区分大小写)?

mysql - 将 mysql 时间戳字段转换为格式化日期 dd/mm/yyyy

php - php mysql中的for循环查询插入

html - 标题位置 CSS

javascript - 无法使用 JS/Jquery 更改背景图片

php - 调用数组 Laravel 5.2 上的成员函数 getClientOriginalName()

php - mySQL 版本之间的语法

PHP Image Zoom - 指数级