根据所选月份过滤数据

时间:2016-09-06 05:27:03

标签: php html sql

我想在我的程序上添加一些额外的功能,用于过滤所选月份的数据库

<div class="content-loader">
       <form method="post" action="index.php"> //select month
<select name="month" id="month">
    <option></option>
    <option value="1">January</option>
    <option value="2">February</option>
    <option value="3">March</option>
    <option value="4">April</option>
    <option value="5">May</option>   //date format on my database yyyy-mm-dd
    <option value="6">June</option>
    <option value="7">July</option>
    <option value="8">August</option>
    <option value="9">September</option>
    <option value="10">October</option>
    <option value="11">November</option>
    <option value="12">December</option>
</select>
<input type="submit" value="submit" name="submit"/>
   </form>
    <table cellspacing="0" width="100%" id="example" class="table table-      striped table-hover table-responsive">
    <thead>
    <tr>
    <th>First Name</th>
    <th>Last Name</th>
    <th>Check In Date</th>   //table head
    <th>Check Out Date</th>
    <th>Room Rate</th>
    <th>Reservation Fee</th>
    <th>Date Paid</th>
    <th>Mode of Paymnet</th>
    <th>Status</th>
    <th>edit</th>
    <th>delete</th>

    </tr>
    </thead>
    <tbody>

所以我修改了它,我收到了这个错误

注意:

中的数组到字符串转换
  

致命错误:未捕获的异常&#39; PDOException&#39;与消息   &#39; SQLSTATE [42S22]:未找到列:1054未知列&#39;数组&#39;在   &#39; where where&#39;&#39;在堆栈跟踪中:#0 PDOStatement-&gt; execute()#1 {main}   投入

<?php
        require_once 'dbconfig.php';

        $stmt = $db_con->prepare("SELECT * FROM tblguest WHERE MONTH(checkin) =".['month']." ");
        $stmt->execute();
        while($row=$stmt->fetch(PDO::FETCH_ASSOC))
        {
            ?>
            <tr>
            <td><?php echo $row['fname']; ?></td>   
            <td><?php echo $row['lname']; ?></td>
            <td><?php echo $row['checkin']; ?></td>
            <td><?php echo $row['checkout']; ?></td>
            <td><?php echo $row['rrate']; ?></td>      //data 
            <td><?php echo $row['reservefee']; ?></td>
            <td><?php echo $row['datepaid']; ?></td>
            <td><?php echo $row['modepayment']; ?></td>
            <td><?php echo $row['stats']; ?></td>
            <td align="center">
            <a id="<?php echo $row['fname']." ".$row['lname']; ?>" class="edit-link" href="#" title="Edit">
            <img src="edit.png" width="20px" />
            </a></td>
            <td align="center"><a id="<?php echo $row['fname']." ".$row['lname']; ?>" class="delete-link" href="#" title="Delete">
            <img src="delete.png" width="20px" />
            </a></td>
            </tr>
            <?php
        }
    ?>
    </tbody>
    </table>

2 个答案:

答案 0 :(得分:0)

在评论中提到的尝试这个

"SELECT * FROM tblguest WHERE MONTH(checkin) =".$_POST['month'];

答案 1 :(得分:0)

你应该写这个。这可能会解决您的问题

$stmt = $db_con->prepare("SELECT * FROM tblguest WHERE MONTH(checkin) = '".$_POST['month']."' ");
    $stmt->execute();
    while($row=$stmt->fetch(PDO::FETCH_ASSOC))
    {
        ?>
        <tr>
        <td><?php echo $row['fname']; ?></td>   
        <td><?php echo $row['lname']; ?></td>
        <td><?php echo $row['checkin']; ?></td>
        <td><?php echo $row['checkout']; ?></td>
        <td><?php echo $row['rrate']; ?></td>      //data 
        <td><?php echo $row['reservefee']; ?></td>
        <td><?php echo $row['datepaid']; ?></td>
        <td><?php echo $row['modepayment']; ?></td>
        <td><?php echo $row['stats']; ?></td>
        <td align="center">
        <a id="<?php echo $row['fname']." ".$row['lname']; ?>" class="edit-link" href="#" title="Edit">
        <img src="edit.png" width="20px" />
        </a></td>
        <td align="center"><a id="<?php echo $row['fname']." ".$row['lname']; ?>" class="delete-link" href="#" title="Delete">
        <img src="delete.png" width="20px" />
        </a></td>
        </tr>
        <?php
    }
    ?>
    </tbody>
    </table>