PHP - 搜索不起作用


PHP - Search Not Working

我试图制作一个搜索引擎,以便在 2 个日期$dateFrom到 $dateTo 之间搜索。
这是我尝试过的:

索引.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>

搜索.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;
 }
 ?>

这是我的数据库连接代码*******

我的数据库名称是工资单,表是出勤。

  1. 您需要在 SQL 代码中命名参数。
  2. 请为列名使用正确的大小写。您拼写了"less_hours",而在数据库中,此列称为"Less_Hours"。这给出了一个通知:"未定义的索引:less_hours"。
  3. 我将输出代码放在 1-st 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>";
        }
    }
}