2014-09-22 72 views
0

我在搜索mysql數據庫的日期字段時遇到問題..我有一個html表單..允許用戶選擇3種不同的方式來搜索數據庫..字段1是student_id,字段2是姓氏,字段3是日期。那麼當我運行該程序,並選擇學生ID,我得到正確的結果回來,當我做同樣的使用姓氏我得到適當的結果回來,但是當我使用日期..我沒有得到任何回報。我碰巧知道結果應該是什麼,因爲我在數據庫中看到它。此外,它與學生ID和姓氏相同的數據記錄。我認爲這可能與格式有關,但我不知道它..如何使用php搜索日期字段 - mysqli

我不知道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> 

[/代碼]

+4

如何被存儲在數據庫中(日期,日期時間,時間戳)的日期? – jmaloney 2014-09-22 00:14:57

+2

停止使用帶有外卡的'LIKE',不妨使用'=' – 2014-09-22 00:47:28

回答

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'