0

I know this is frequently asked question however I am trying to add some code on existing, rest all POST are working fine, just what I am adding is not, I have tried most of the steps but of no use, Error Message I am getting

Notice: Undefined index: pay_date_from in C:\xampp\htdocs\water_inventory\production\fetch_Cust_WaterBillPay.php on line 19

Notice: Undefined index: pay_date_from_to in C:\xampp\htdocs\water_inventory\production\fetch_Cust_WaterBillPay.php on line 21

here is my code:

<form id="demo-form2" class="form-horizontal form-label-left" action="fetch_Cust_WaterBillPay.php" method="POST">
           <div class="form-group">             
                <label class="col-md-2">Pay Date From </label>
                   <div class="col-md-3">
                      <input type="date" id="pay_date_from" name="pay_date_from" class="form-control col-md-7 col-xs-12">
                    </div>
                    <label class="col-md-2">To</label>
                    <div class="col-md-3">
                      <input type="date" id="pay_date_to" placeholder="Enter Current Reading" name="pay_date_from_to" class="form-control col-md-7 col-xs-12">
                   </div>
                <div class="col-md-2">
                    <input type="submit" class="btn btn-block btn-primary searchDept" style="margin-left: 3%;" value="Search"></div>
            </div>
            <br><hr>
              <div id="result"></div><div style="clear:both"></div>
</form>

My fetch_Cust_WaterBillPay.php:

<?php
    session_start();
    include './chklog.php';
    include './db_config.php';

   if ((!isset($_SESSION['first_name']) == true)) {
      unset($_SESSION['first_name']);
      }
  $logged = $_SESSION['first_name'];
  if(isset($_POST['submit']))
  {
    //if(isset($_POST['pay_date_from']))//Tried these 
    $pay_from=$_POST['pay_date_from'];
   //if(isset($_POST['pay_date_from_to']))
   $pay_to=$_POST['pay_date_from_to'];
   $branch_name = $_SESSION['branch_name'];
  }
?>

Getting error on this line:

 <?php
       $res1 =mysqli_query($con, "SELECT cust_id,customer_name,meter_no,created_on,invoice_month,invoice_no,amount_paying,mode_of_payment FROM `wb_customer_payment` WHERE branch_name='$branch_name' and created_on>='$pay_from' and created_on<='$pay_to' order by created_on desc");//Getting error here 
       while($data = mysqli_fetch_array($res1))
       {
         echo "<tr>"
         . "<td>".$data['customer_name']."</td>"
         . "<td>".$data['meter_no']."</td>"
         . "<td>".$data['created_on']."</td>"
         . "<td>".$data['invoice_month']."</td>"
         . "<td>".$data['invoice_no']."</td>"
         . "<td>".$data['amount_paying']."</td>"
         . "<td>".$data['mode_of_payment']."</td>"                                              
         . "<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href='water_bill_detail.php?cust_id=".$data["cust_id"]."' class='btn btn-primary btn-xs' role='button'><i class='fa fa-user'></i> Details</a></td>"
        . "</tr>";
       }
    ?>

UPDATE:
I declared variable just before checking empty $pay_from=''; $pay_to='';
However , now no error however I am not getting values. Even URL doesn't have values passed.

I am not sure what have I am missing. Thanks

mark
  • 623
  • 3
  • 21
  • 54

1 Answers1

0

Instead of using date directly from user input field convert it to proper format

Use like

if(isset($_POST['submit']))
  {
    $pay_from = date('Y-m-d',strtotime($_POST['pay_date_from']));
    $pay_to = date('Y-m-d',strtotime($_POST['pay_date_from_to']));
    $branch_name = $_SESSION['branch_name'];
  }
TarangP
  • 2,711
  • 5
  • 20
  • 41
  • Used this now I am getting `Notice: Undefined variable: pay_from in C:\xampp\htdocs\water_inventory\production\fetch_Cust_WaterBillPay.php on line 176` `Notice: Undefined variable: pay_to in C:\xampp\htdocs\water_inventory\production\fetch_Cust_WaterBillPay.php on line 176` – mark Dec 22 '18 at 06:54
  • My answer is just an example of one field.you have use above function in all place where you are usig dates – TarangP Dec 22 '18 at 06:55
  • yes did same for both variables – mark Dec 22 '18 at 06:57
  • so what happen ? – TarangP Dec 22 '18 at 06:59
  • same as mentioned in first comment of your answer. However, I have declared variable `$pay_from=''; $pay_to='';` now no error. But not getting value – mark Dec 22 '18 at 07:11
  • i have updated answer.and also check database values that it was in `YYYY-MM-DD` Format – TarangP Dec 22 '18 at 07:15
  • yes its in `YYYY-MM-DD`, however now I am not getting error but not getting values either – mark Dec 22 '18 at 07:19
  • I prefer `$pay_date_from = (new \DateTime($_POST['pay_date_from']))->format('Y-m-d');` myself, but same deal. – ArtisticPhoenix Dec 22 '18 at 08:14