初始(未定义索引)错误,以后会消失,为什么?

时间:2015-05-11 18:57:17

标签: php html pdo

我试图通过html表单输入日期范围,并通过PDO方法输出与输入日期对应的结果。当我运行脚本时,我收到以下错误消息。虽然当我尝试输入日期并按提交时,错误消息消失,我成功获得了我想要的结果。

  
      
  1. 未定义的索引:from_date
  2.   
  3. 未定义的索引:to_date
  4.   

为什么我得到这个初始错误消息后消失?请指教!我是初学者。

<?php
require_once 'db_alternate2.php';
session_start(); 

if (isset($_POST['submit'])) {
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$fromDate = $_POST['from_date'];    
$toDate = $_POST['to_date'];        

$sql = "SELECT * FROM `student_db` WHERE `stu_registered_date` BETWEEN   
'$fromDate' AND '$toDate'"; 


$q = $conn->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);

} catch (PDOException $pe) {
die("Could not connect to the database $dbname :" . $pe->getMessage());
}
}
?> 

我的HTML表单输入

    <form action="student_sort.php" method="post">
    <div class="form-left">
      <p>From date</p> <input type="date" name="from_date" placeholder="From date"/>
    </div>
    <div class="form-right">
       <p>To date</p> <input type="date" name="to_date" placeholder="To date"/>
    </div>
    <div class="form-centre">
       <input class="submit" type="submit" value="search"/>         
    </div>      
    </form>

和我在同一页面的PHP输出是

   <tbody><?php while ($r = $q->fetch()): ?>
    <tr>
      <td><?php echo htmlspecialchars($r['stu_call_received']); ?></td>  
      <td><?php echo htmlspecialchars($r['stu_counselled_by']); ?></td>
      <td><?php echo htmlspecialchars($r['stu_due_date']); ?></td>
    </tr>
    <?php endwhile; ?>
    </tbody>

0 个答案:

没有答案