0

I am trying to get a date range input through html form and output results corresponding to the input dates through PDO method. When i run the script, i am getting the below error msg. Although when i try to input dates and press submit, the error msg disappears and i am successfully getting my desired results.

  1. Undefined index: from_date
  2. Undefined index: to_date

Why am i getting this initial error msg which disappears later? Please advise! i am a beginner.

<?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());
}
}
?> 

my HTML form input

    <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>

and my PHP output in the same page is

   <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>
Franklin Vaz
  • 19
  • 1
  • 2
  • 9

0 Answers0