获取未定义的索引:月错误

时间:2016-09-07 07:26:23

标签: php html sql

程序运行正常,但在程序的第一次运行中它有一个通知:未定义的索引:但如果我提交一个选定的选项,它将消失

    <form method="post" action="index.php">
    <select name="months" id="months">//months
    <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>
    <option value="6">June</option>   //date values that I want to be called 
    <option value="7">July</option>   //to filter the data on selected date
    <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>
    <th>Check Out Date</th>  //table
    <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>

    //

已经尝试过isset仍然具有相同的输出

    <?php
    require_once 'dbconfig.php';
    if (isset($_POST['months'])) {
        $months = $_POST['months'];      //also tried to use isset
    }                           
    $stmt = $db_con->prepare("SELECT * FROM tblguest WHERE MONTH(checkin) ='".$_POST['months']."' ");
    $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>
        <td><?php echo $row['reservefee']; ?></td>   //data selected column 
        <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)

点击这里

if (isset($_POST['months'])) {
    $months = $_POST['months'];      //also tried to use isset
}else{
    $months='';
}                           
$stmt = $db_con->prepare("SELECT * FROM tblguest WHERE MONTH(checkin) ='".$months."' ");

答案 1 :(得分:0)

由于您提交的是表单,因此您应该使用isset($_POST['submit']),即是否单击了表单提交按钮。

<?php 
require_once 'dbconfig.php';

if (isset($_POST['submit'])) {

    $months = $_POST['months'];   

    $stmt = $db_con->prepare("SELECT * FROM tblguest WHERE MONTH(checkin) ='".$_POST['months']."' ");

    $stmt->execute();

   while($row=$stmt->fetch(PDO::FETCH_ASSOC)) {   
?>   

   <!-- Write your HTML Table code here -->


 <?php } } ?>

在这种情况下,只有在提交表单并提取记录时,才会显示整个HTML表格。