0

This is the main.php with a date picker and is pointing to index.php where I have the table with data. It work when I pick a date but when I make some changes to the index.php page ex: edit data or if I go directly to index.php I get :

Notice: Undefined index: arr_date in /Applications/XAMPP/xamppfiles/htdocs/zeta/index.php on line 112

<?php

include 'database.php';

$reponse = $bdd->query("SELECT * FROM live");  

$reponse->closeCursor();

?>

  <form action="index.php" method="post">
    <input type="date"  name="arr_date" value="FROM">
    <input type="submit"  value="Search">
  </form>

index.php

 <?php
 include 'database.php';

 $selected_date= $_POST['arr_date'];

 $reponse = $bdd->query("SELECT * FROM live where 
 arr_date='$selected_date'");

while ($row = $reponse->fetch())
{
?>

Hi everybody, thank you for your help, for info this solved my problem:

<?php
session_start();
include 'db.php';

// Check Post variables are available
if(isset($_POST['select_date']))
{
     $_POST['select_date']."";
     // Set session variables
    $_SESSION["select_date"] = $_POST['select_date'];
     $_SESSION["select_date"]."";;
}
else
     $_SESSION["select_date"];
    //echo 'No, form submitted.';

$select_date = $_SESSION["select_date"];


$reponse = $bdd->query("SELECT * FROM live where 
select_date='$select_date'");

while ($row = $reponse->fetch())
{
scunja
  • 37
  • 6
  • 4
    Possible duplicate of [Undefined index error PHP](https://stackoverflow.com/questions/10613570/undefined-index-error-php) – Albzi Jul 18 '17 at 10:49
  • first try to print `$_POST`, and handle this type of things by using `isset()` function. – Sonu Bamniya Jul 18 '17 at 10:49
  • 1
    your SQL is vulnerable to injection attacks. Learn how to use parameterised queries and prepared statements. http://bobby-tables.com/ has a clear (and humorous) explanation of the risks, plus some info on how to do it properly, including in PHP. – ADyson Jul 18 '17 at 10:52

7 Answers7

1

Of course, if you go directly to the index.php it is a new request which does not have the form data.

You should check if the form is submitted

if(isset($_POST['arr_date'])) {
    //do stuff
}
Dwayne Charrington
  • 6,524
  • 7
  • 41
  • 63
katona.abel
  • 771
  • 4
  • 12
  • thx is working no more error but no more data on the page even with refresh, I have to go back and refresh to display data – scunja Jul 18 '17 at 11:02
  • @scunja You have to output the form again if it wasn't a POST (and also if the entered values are invalid, eg. in your case the date is empty or not parsable) – Daniel Alder Jul 18 '17 at 11:57
  • how to solve that? i don't want to submit the form again. Is there a way to keep the index with the $_POST['arr_date] from the form – scunja Jul 18 '17 at 12:06
  • if you're interested Ive posted the solution – scunja Jul 19 '17 at 12:38
1

Use isset() or empty()

<?php
 include 'database.php';

 if(isset($_POST['arr_date']))
 {
   $selected_date= $_POST['arr_date'];

   $reponse = $bdd->query("SELECT * FROM live where 
   arr_date='$selected_date'");

  while ($row = $reponse->fetch())
  {
  }
 }

echo "Other code which is not depended on POST will write here";
?>
RJParikh
  • 4,096
  • 1
  • 19
  • 36
  • thx is working no more error but no more data on the page even with refresh, I have to go back and refresh to display data – scunja Jul 18 '17 at 11:02
  • Where you want to display? on index page ? @scunja – RJParikh Jul 18 '17 at 11:05
  • When you click submit then form will redirect to index.php page and it will surely give you post data after submit and display it on index.php – RJParikh Jul 18 '17 at 11:13
  • Yes that's right I have post data but when i edit something on index.php after form submit no more data on the page – scunja Jul 18 '17 at 11:25
  • Write your other code after complete `if(isset($_POST['arr_date']))` and then check.. Check edited answer @scunja – RJParikh Jul 18 '17 at 11:33
  • I have other form on index.php that change background color of cell, update the cells data, cancel a row etc... so if I perform any of this form the index.php will be empty – scunja Jul 18 '17 at 11:34
  • Check edited code and last comment hope it will help... If you still not able to find solution then please paste whole index file in question – RJParikh Jul 18 '17 at 11:36
  • if you're interested Ive posted the solution – scunja Jul 19 '17 at 12:38
  • where you have posted ? :) – RJParikh Jul 19 '17 at 12:45
  • at the top of the page, at the end of the original post – scunja Jul 19 '17 at 13:19
1

You need to check whether the variable is set or not.

If it is set, then only go ahead with isset()

Corrected Code:

<?php
include 'database.php';
if (isset($_POST['arr_date'])) {
 $selected_date= $_POST['arr_date'];
 $reponse = $bdd->query("SELECT * FROM live where 
 arr_date='$selected_date'");
 while ($row = $reponse->fetch()) {
  // While code
 }
}
?>
Pupil
  • 23,834
  • 6
  • 44
  • 66
  • thx is working no more error but no more data on the page even with refresh, I have to go back and refresh to display data – scunja Jul 18 '17 at 11:02
  • @scunja unless you go there via the submission of your form from the other page, then of course there will be no more data. Visiting index.php directly without form submission makes no sense, because it essentially relies on the data coming from the form. There's no reason to view the page otherwise. – ADyson Jul 18 '17 at 11:37
  • @ADyson thx but how to correct this stuff? I really don't know – scunja Jul 18 '17 at 11:39
  • @scunja I don't know what you want to correct? If your index.php relies on data from the form submission, then why would you try to visit it any other way than via submitting the form? – ADyson Jul 18 '17 at 11:41
  • yes but after the form submission i want to saty on the index.php and perform other form action on the same page (index.php) – scunja Jul 18 '17 at 11:45
  • if you're interested Ive posted the solution – scunja Jul 19 '17 at 12:38
1

You have to show full code. Whatever probably you are fetching value from array directly like this. $val= $_POST['arr_date'], you have to use isset() function to check if variable is set or not like this.

if(isset($_POST['arr_date'])){
   $var = $_POST['arr_date'];
}
Davinder Kumar
  • 652
  • 4
  • 17
  • thx is working no more error but no more data on the page even with refresh, I have to go back and refresh to display data – scunja Jul 18 '17 at 11:03
  • Sorry? , it will show data only when you have submit a form with action "index.php" as i am assuming that this code is on index.php – Davinder Kumar Jul 18 '17 at 11:33
0

Try to see if the post data is set or not

<?php
 include 'database.php';
 $selected_date= "";//set select date to blank
 if(isset($_POST['arr_date']))
 {
     $selected_date= $_POST['arr_date'];
     $reponse = $bdd->query("SELECT * FROM live where arr_date='$selected_date'");

    while ($row = $reponse->fetch())
    {
    }//end of while
}
?>

What I've done is used if condition

$selected_date= "";//set select date to blank
 if(isset($_POST['arr_date']){
     $selected_date= $_POST['arr_date'];
 }
ADyson
  • 57,178
  • 14
  • 51
  • 63
Regolith
  • 2,944
  • 9
  • 33
  • 50
  • your code is wrong. it will emit a "Undefined index" as you check the value first(before isset) – katona.abel Jul 18 '17 at 10:54
  • 1
    i am not checking the value before `isset`. I am assigning blank data to `$selected_date` as it if used below the code and if `if` statement is not reached it will not generate any error. – Regolith Jul 18 '17 at 11:00
  • `$selected_date= "";` is pretty much redundant I think – ADyson Jul 18 '17 at 11:35
0

Direct access to undefined array key (array index) results in

"Notice: Undefined index: KEY_NAME".

Solution:

  1. First check if key exists isset($_POST['KEY'])

  2. If exists, use

Code:

if (isset($_POST['KEY'])) {
    // use $_POST['key']
    };
RJParikh
  • 4,096
  • 1
  • 19
  • 36
mirec_c
  • 27
  • 1
  • 7
0

Your code is almost right but you need to write validation also as follow:

1st Method :

<?php
 include 'database.php';

 $selected_date= isset($_POST['arr_date'])?$_POST['arr_date']:"";
 if(!empty($selected_date)){
 $reponse = $bdd->query("SELECT * FROM live where 
 arr_date='$selected_date'");

  while ($row = $reponse->fetch())
  {
    // Do your stuff here;
   }
 }
?>

2nd Method :

<?php
 include 'database.php';
 $selected_date= "";//set select date to blank
 if(isset($_POST['arr_date']))
 {
     $selected_date= $_POST['arr_date'];
     $reponse = $bdd->query("SELECT * FROM live where arr_date='$selected_date'");

    while ($row = $reponse->fetch())
    {

       // Do your stuff here;
       }
     }
?>
Prabhu Nandan Kumar
  • 1,205
  • 12
  • 22