2

Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”

Below I have a piece of code where it contains a form where it contains 2 text inputs and a drop down menu which contains a list of courses retrieved from a query.

I am trying to use this line of code below to determine if the data is successful or not so that if it is not successful ($numrowsstmt != 1), then it keeps the selected option in the drop down menu selected. If successful then ut goes back to Please Select option

if ($validSubmission && $course == $_POST['courses'] && $numrowsstmt != 1) { 

The problem I am having though is that I am getting an udefined variable for $numrowsstmt in the line above and the reason I am getting this is because the variable is called later on in the code.

What my question is that how should the code/logic be shuffled and look like so that it removes the undefined variable error yet be able to do what it is suppose to do? If this code below can be shuffled then I can change the logic in all my pages to be able to follow the logic in your answer:

<?php
// required variables (make them explciit no need for foreach loop)
$getfirstname = (isset($_POST['firstname'])) ? $_POST['firstname'] : '';
$getsurname   = (isset($_POST['surname'])) ? $_POST['surname'] : '';
$getcourse    = (isset($_POST['courses'])) ? $_POST['courses'] : '';
$errormsg     = (isset($errormsg)) ? $errormsg : '';

$validSubmission = isset($_POST['registerbtn']) && isset($getfirstname) && isset($getsurname) && isset($getcourse);


$courseactive = 1;

$sql = "SELECT CourseId, CourseNo, CourseName FROM Course WHERE CourseActive = ? ORDER BY CourseNo";

$sqlstmt = $mysqli->prepare($sql);

$sqlstmt->bind_param("i", $courseactive);

$sqlstmt->execute();

$sqlstmt->bind_result($dbCourseId, $dbCourseNo, $dbCourseName);

$courses = array(); // easier if you don't use generic names for data 

$courseHTML = "";
$courseHTML .= '<select name="courses" id="coursesDrop">' . PHP_EOL;
$courseHTML .= '<option value="">Please Select</option>' . PHP_EOL;

while ($sqlstmt->fetch()) {
    $course     = $dbCourseId;
    $courseno   = $dbCourseNo;
    $coursename = $dbCourseName;
    if ($validSubmission && $course == $_POST['courses'] && $numrowsstmt != 1) { //error here
        $courseHTML .= "<option value='" . $course . "' selected='selected'>" . $courseno . " - " . $coursename . "</option>" . PHP_EOL;
    } else {
        $courseHTML .= "<option value='" . $course . "'>" . $courseno . " - " . $coursename . "</option>" . PHP_EOL;
    }

}

$courseHTML .= '</select>';


if ((isset($_POST['registerbtn']))) {
    $getfirstname = $_POST['firstname'];
    $getsurname   = $_POST['surname'];
    $getcourse    = $_POST['courses'];

    if ($getfirstname) {
        if ($getsurname) {
            if ($getcourse) {
                // don't use $mysqli->prepare here
                $query = "SELECT StudentUsername FROM Student WHERE StudentUsername = ?";
                // prepare query
                $stmt  = $mysqli->prepare($query);
                // You only need to call bind_param once
                $stmt->bind_param("s", $getfirstname);
                // execute query
                $stmt->execute();
                // get result and assign variables (prefix with db)
                $stmt->bind_result($dbStudentUsername);
                //get number of rows
                $stmt->store_result();
                $numrowsstmt = $stmt->num_rows();

                if ($numrowsstmt == 1) {
                    $errormsg     = "<span style='color: green'>Student " . $getusername . " - " . $getfirstname . " " . $getsurname . " has been Registered</span>";
                    $getfirstname = "";
                    $getsurname   = "";
                    $getcourse    = "";

                } else {
                    $errormsg = "An error has occured, Student has not been Registered";

                }

            }

            else {
                $errormsg = "There is already a Student with that Username";
            }
        }

        else {
            $errormsg = "You must select the Student's Course to Register";
        }
    }

    else {
        $errormsg = "You must enter in Student's Surname to Register";
    }
}

else {
    $errormsg = "You must enter in Student's First Name to Register";
}


$form = " 
<form action='" . htmlentities($_SERVER["PHP_SELF"]) . "' method='post'> 
  <table> 
  <tr> 
  <td></td> 
  <td id='errormsg'>$errormsg</td> 
  </tr> 
  <tr> 
  <td>First Name:</td> 
  <td><input type='text' name='firstname' value='$getfirstname' /></td> 
  </tr> 
  <tr> 
  <td>Surname:</td> 
  <td><input type='text' name='surname' value='$getsurname' /></td> 
  </tr> 
  <tr> 
  <td>Course:</td> 
  <td>{$courseHTML}</td> 
  </tr> 
  <tr> 
  <td></td> 
  <td><input type='submit' value='Register' name='registerbtn' /></td> 
  </tr> 
  </table> 
  </form>";

echo $form;



?> 
</body> 
</html>
Community
  • 1
  • 1
Manixman
  • 307
  • 6
  • 19

2 Answers2

0

Above your isset($_POST['registerbtn']) check, set $numrowsstmt to null or some other falsy value.

Move the construction of the $courseHTML to below the isset($_POST['registerbtn']) check, above the construction of $form.

This way, the variable is in scope and has a chance of actually having a value. How did you expect this to work otherwise?

Charles
  • 50,943
  • 13
  • 104
  • 142
0

The isset function is what you want, or possibly empty.

e.g

if ($validSubmission && $course == $_POST['courses'] && (isset($numrowsstmt) && $numrowsstmt != 1) {

Rimu Atkinson
  • 775
  • 4
  • 15