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

时间:2014-09-22 00:09:53

标签: php html mysql html5 mysqli

我在搜索mysql数据库的日期字段时遇到问题。我有一个html表单..允许用户选择3种不同的方式来搜索数据库..字段1是student_id,字段2是姓氏,字段3是日期。好吧,当我运行程序并选择学生ID时,我得到了正确的结果,当我使用姓氏做同样的事情时我得到了正确的结果,但是当我使用date ...我没有得到任何回报。我碰巧知道结果应该是什么,因为我在数据库中看到它...除了它与学生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>

[/代码]

1 个答案:

答案 0 :(得分:0)

我相信你正在使用喜欢获得该会话或月份的考试日期,以便您可以使用它 使用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'