2014-12-26 83 views
-2

我試圖做一個搜索引擎,以便在2個日期$ dateFrom到$ dateTo之間搜索。
這裏是我曾嘗試:PHP - 搜索不工作

的index.php:

<?php 
    require_once 'Connection.simple.php'; 
    $tutorialTitle = "Using Ajax to search a Record with PHP, MySQL and jQuery (Look and Feel by Bootstrap)"; 

    $conn = dbConnect(); 
?> 
<!DOCTYPE html> 
<html lang="en"> 
    <head> 
     <meta charset="UTF-8" /> 
     <title><?php echo $tutorialTitle;?></title> 
     <meta http-equiv="X-UA-Compatible" content="IE=9" /> 
     <meta name="copyright" content="BEHSTANT SOFTWARE | Datasoft Engineering 2013"/> 
     <meta name="author" content="Reedyseth"/> 
     <meta name="email" content="ibarragan at behstant dot com"/> 
     <meta name="description" content="<?php echo $tutorialTitle;?>" /> 
     <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
     <link rel=stylesheet href="css/style01.css"> 
     <!-- Bootstrap --> 
     <link href="css/bootstrap.min.css" rel="stylesheet"> 
    </head> 
    <body> 
     <div class="wrapper"> 
      <div class="page-header "> 

       <div class="panel panel-default"> 

       </div> 
      </div> 

      <div class="mainContent"> 
       <form class="form-horizontal" role="form" method="get"> 
        <div class="form-group"> 
         <label class="col-sm-2 control-label" for="minimum date">employee_id</label> 
         <div class="input-group col-sm-9"> 
          <input id="DateFrom" name="DateFrom" type="date" class="form-control" placeholder="Type the name" /> 
          <input id="DateTo" name="DateTo" type="date" class="form-control" placeholder="Type the name" /> 
          <span class="input-group-btn"> 
            <button type="button" class="btn btn-default btnSearch"> 
             <span class="glyphicon glyphicon-search"> Search</span> 
            </button> 
          </span> 
         </div> 
        </div> 
       </form> 
       <div class="col-sm-2"></div> 
       <div class="col-sm-8"> 
       <!-- This table is where the data is display. --> 
        <table id="resultTable" class="table table-striped table-hover"> 

         <tbody></tbody> 
        </table> 
       </div> 
      </div> 
     </div> 
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> 
    <script src="js/jquery-1.10.2.js"></script> 
    <!-- Include all compiled plugins (below), or include individual files as needed --> 
    <script src="js/bootstrap.min.js"></script> 
    <script type="text/javascript"> 
     jQuery(document).ready(function($) { 
      $('.btnSearch').click(function(){ 
       makeAjaxRequest(); 
      }); 

      $('form').submit(function(e){ 
       e.preventDefault(); 
       makeAjaxRequest(); 
       return false; 
      }); 

      function makeAjaxRequest() { 
       $.ajax({ 
        url: 'search.php', 
        type: 'get', 
        DateFrom: {DateFrom: $('input#DateFrom').val()}, 
        DateTo: {DateTo: $('input#DateTo').val()}, 
        success: function(response) { 
         $('table#resultTable tbody').html(response); 
        } 
       }); 
      } 
     }); 
    </script> 
    </body> 
</html> 

的search.php:

<?php 

    require_once 'Connection.simple.php'; 
    $conn = dbConnect(); 
    $OK = true; 
    if (isset($_GET['DateFrom']) && isset($_GET['DateTo'])) {  
    $dateFrom = $_GET['DateFrom']; 
    $dateTo = $_GET['DateTo']; 


    $sql = "SELECT * FROM attendance WHERE date >= '". $dateFrom ."' AND date <= '". $dateto ."' "; 
    } 
    if(empty($rows)) { 
     echo "<tr>"; 
      echo "<td colspan='4'>There were not records</td>"; 
     echo "</tr>"; 
    } 

    else { 
     foreach ($rows as $row) { 
      echo "<tr>"; 
       echo "<td>".$row['emp_id']."</td>"; 
       echo "<td>".$row['Date']."</td>"; 
       echo "<td>".$row['day']."</td>"; 
       echo "<td>".$row['time_in']."</td>"; 
       echo "<td>".$row['time_out']."</td>"; 
       echo "<td>".$row['worked']."</td>"; 
       echo "<td>".$row['overtime']."</td>"; 
       echo "<td>".$row['less_hours']."</td>"; 
       echo "<td>".$row['transport_in']."</td>"; 
       echo "<td>".$row['Transport_out']."</td>"; 


      echo "</tr>"; 
     } 
    } 

?> 

編輯1:

$sql = "SELECT * FROM attendance WHERE date >= '". $dateFrom ."' AND date <= '". $dateto ."' "; 
     // we have to tell the PDO that we are going to send values to the query 
     $stmt = $conn->prepare($sql); 
     // Now we execute the query passing an array toe execute(); 
     $results = $stmt->execute(array($dateFrom, $dateTo)); 
     // Extract the values from $result 
     $rows = $stmt->fetchAll(); 
     $error = $stmt->errorInfo(); 

*** ***我補充說,使執行查詢,但仍然沒有牛逼工作******

<?php 
function dbConnect(){ 
    $conn = null; 
    $host = 'localhost'; 
    $db = 'payroll'; 
    $user = 'root'; 
    $pwd = ''; 
    try { 
     $conn = new PDO('mysql:host='.$host.';dbname='.$db, $user, $pwd); 
     //echo 'Connected succesfully.<br>'; 
    } 
    catch (PDOException $e) { 
     echo '<p>Cannot connect to database !!</p>'; 
     echo '<p>'.$e.'</p>'; 
     exit; 
    } 
    return $conn; 
} 

?> 

*******這是我的DbConnection *******

代碼我的數據庫名稱爲工資和表出席。

+0

你更容易,如果你告訴我們你希望具體是什麼,這個代碼做,它實際上做什麼,以獲得幫助。 –

+0

你得到什麼錯誤? –

+0

該代碼用於在日期之間出席。我期望從一個日期到另一個日期有2個文本框(輸入),當查詢執行時,表格會在該日期之間獲取數據庫中的所有列 – akki2401

回答

0
  1. 您需要命名SQL代碼中的參數。
  2. 請爲列名使用正確的大小寫。你拼寫「less_hours」,而在你的數據庫中,這個列被稱爲「Less_Hours」。這給了一個通知:「未定義的索引:less_hours」。
  3. 我把輸出代碼放在第一個if裏面,否則$rows第一次總是不確定的。

此代碼爲我工作:

if (isset($_GET['DateFrom']) && isset($_GET['DateTo'])) { 
    $dateFrom = $_GET['DateFrom']; 
    $dateTo = $_GET['DateTo']; 


    $sql = "SELECT * FROM attendance WHERE 
      date >= :date_from AND date <= :date_to "; 

    $stmt = $conn->prepare($sql); 
    // Now we execute the query passing an array toe execute(); 
    $results = $stmt->execute(
         array('date_from' => $dateFrom, 'date_to' => $dateTo)); 
    // Extract the values from $result 
    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); 

    if(empty($rows)) { 
     echo "<tr>"; 
     echo "<td colspan='4'>There were not records</td>"; 
     echo "</tr>"; 
    } 

    else { 
     foreach ($rows as $row) { 

      echo "<tr>"; 
      echo "<td>".$row['emp_id']."</td>"; 
      echo "<td>".$row['Date']."</td>"; 
      echo "<td>".$row['Day']."</td>"; 
      echo "<td>".$row['Time_In']."</td>"; 
      echo "<td>".$row['Time_Out']."</td>"; 
      echo "<td>".$row['Worked']."</td>"; 
      echo "<td>".$row['Overtime']."</td>"; 
      echo "<td>".$row['Less_Hours']."</td>"; 
      echo "<td>".$row['Transport_In']."</td>"; 
      echo "<td>".$row['Transport_Out']."</td>"; 

      echo "</tr>"; 
     } 
    } 
} 
+0

先生它仍然沒有工作。我已經替換了所有除了:** require_once'Connection.simple。PHP的「; $ conn = dbConnect(); $ OK = true; ** – akki2401

+0

@ akki2401嘗試對日期進行硬編碼:$ dateFrom ='2014-01-01',$ dateTo ='2014-12-31' – user4035

+0

不工作 – akki2401