0

I keep getting an the undefined index error for the workoutDate variable, even though It still puts a value into the database. I just want to get rid of the error because it is annoying. Sorry if I'm too vague.

session_start();
include'connect.inc.php';
if(!isset($_POST['exercise']))
{
        showForm();

} 
    else
{
        processExercise();       

}
function showForm()
{
$self = htmlentities($_SERVER['PHP_SELF']);     

    echo("<form id='exerciseForm' action = '$self' method = 'POST'>
          <fieldset>
          <legend><strong>Exercise entry Form</strong></legend>");

    echo("<table border='1'>");
    echo("<tr><td>Workout name</td><td><select name = 'workoutName'><option value='cycling'>Cycling</option><option value='swimming'>Swimming</option><option value='running'>Running</option><option value='gym'>Gym</option></td></tr>    
          <tr><td>Date </td><td><input name = 'workoutDate' type='button' id='datepicker'  value = '".date('d-m-Y')."'/></td></tr>
          <tr><td>Duration (mins)</td><td><input name = 'workoutDuration' type = 'text' id = 'workoutDuration'></td></tr>      
          <tr><td>Distance (if appicable)</td><td><input name = 'workoutDistance' type = 'text' id = 'workoutDistance'></td></tr>  
          <tr><td>Comment</td><td><input name = 'workoutComment' type = 'text' id = 'workoutComment'></td></tr>
          ");

    echo("</tr>");
    echo("</table>");
    echo("<input type='submit' name='exercise' id ='exercise' value = 'Add exercise'>
          </fieldset>
          </form>");

}
function processExercise()
{
            var_dump(isset($_POST['workoutDate'])); 
        $workoutName = $_POST['workoutName'];
    $workoutDate = $_POST['workoutDate'];
    $workoutDuration = $_POST['workoutDuration'];
    $workoutDistance = $_POST['workoutDistance'];
    $workoutComment = $_POST['workoutComment'];
    $currentuserID = ($_SESSION['userID']);

    //strip slashes and tags
    //$workoutName = stripslashes($workoutName);
            $workoutName = mysql_real_escape_string($workoutName);
    $workoutName = strip_tags($_POST['workoutName']); 

    //$workoutDate = stripslashes($workoutDate);
            //$workoutDate = mysql_real_escape_string($workoutDate);
    //$workoutDate = strip_tags($_POST['workoutDate']);

    $workoutDuration = stripslashes($workoutDuration);
            $workoutDuration = mysql_real_escape_string($workoutDuration);
    $workoutDuration = strip_tags($_POST['workoutDuration']);

    $workoutDistance = stripslashes($workoutDistance);
            $workoutDistance = mysql_real_escape_string($workoutDistance);
    $workoutDistance = strip_tags($_POST['workoutDistance']);

    $workoutComment = stripslashes($workoutComment);
            $workoutComment = mysql_real_escape_string($workoutComment);
    $workoutComment = strip_tags($_POST['workoutComment']);


            if($workoutDate == "")
            {
                $workoutDate = date('Y-m-d');
            }
            $phpDate = strtotime($workoutDate);
            $date = date('Y-m-d',$phpDate);

            //Auto comment
            if($workoutComment == "")
            {                    
                $workoutComment = "Todays workout was $workoutName" ;                    
            }

    $insertQuery = "INSERT into workout (workoutName, workoutDate, workoutDuration, workoutDistance, workoutComment, userID)
                          values ('$workoutName', '$date', '$workoutDuration', '$workoutDistance', '$workoutComment', '$currentuserID')";

             $result = mysql_query($insertQuery);
            $count = mysql_insert_id();

    showForm();
}
user3016302
  • 3
  • 1
  • 2
  • 1
    Try Use mysqli_* function or PDO – Krish R Nov 21 '13 at 06:47
  • the error is on Line 104 – user3016302 Nov 21 '13 at 06:49
  • Duplicate of [Reference - What does this error mean in PHP?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) and [here's the answer](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12770836#12770836) you are looking for. – Leri Nov 21 '13 at 06:49

3 Answers3

0

change input type from button into text i.e type='button' into type='text' for workoutDate input field,

Use <input name = 'workoutDate' type='text' id='datepicker' value = '".date('d-m-Y')."'/>' instead of <input name = 'workoutDate' type='button' id='datepicker' value = '".date('d-m-Y')."'/>

        if(!isset($workoutDate) && $workoutDate == "")
        {
            $workoutDate = date('Y-m-d');
        }
Krish R
  • 22,583
  • 7
  • 50
  • 59
0

Check variable is set or not while using it

       if(!isset($workoutDate) && $workoutDate == "")
        {
            $workoutDate = date('Y-m-d');
        }
        $phpDate = strtotime($workoutDate);
        $date = date('Y-m-d',$phpDate);

        //Auto comment
        if(!isset($workoutComment) && $workoutComment == "")
        {                    
            $workoutComment = "Todays workout was $workoutName" ;
        }
Roopendra
  • 7,674
  • 16
  • 65
  • 92
0

Undefined index is not an error it is a warning

function processExercise()
{
    var_dump(isset($_POST['workoutDate'])); // first line 
    $workoutName = $_POST['workoutName']; // second line

Now consider this two lines out of your code.

If the first line returns false means that $_POST['workoutDate'] is not set.. In that case the second line will always return that annoying warning Undefined index ...

The easy way is to use this function error_reporting

error_reporting( E_WARNING );

This function will not show any warnings in your script.

and the other way is to use the function isset before using such variables

$workoutDate = "";
if ( isset( $_POST['workoutDate'] ) ) {
    $workoutDate = $_POST['workoutDate'];
} else {
    $workoutDate = date('Y-m-d');
}

so if the $_POST['workoutDate'] is set and available to use. It will assign to $workoutDat else you can assign some default value or generate some error/warning message and abort the operation.. Like in above case we are assigning date('Y-m-d'); as a default value.

zzlalani
  • 22,960
  • 16
  • 44
  • 73