Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8971167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:53:45+00:00 2026-06-15T17:53:45+00:00

Possible Duplicate: PHP: Notice: Undefined variable and Notice: Undefined index Below I have a

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:53:46+00:00Added an answer on June 15, 2026 at 5:53 pm

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: PHP: “Notice: Undefined variable” and “Notice: Undefined index” This code shows warnings
Possible Duplicate: PHP: Notice: Undefined variable and Notice: Undefined index In html code: <select
Possible Duplicate: PHP: Notice: Undefined variable and Notice: Undefined index I've created a form
Possible Duplicate: PHP: “Notice: Undefined variable” and “Notice: Undefined index” I have a member.php
Possible Duplicate: PHP: Notice: Undefined variable and Notice: Undefined index I have spent around
Possible Duplicate: PHP: Notice: Undefined variable and Notice: Undefined index I know its basic
Possible Duplicate: PHP: Notice: Undefined variable and Notice: Undefined index I am just learning
Possible Duplicate: PHP: Notice: Undefined variable and Notice: Undefined index Reference - What does
Possible Duplicate: PHP: Notice: Undefined variable and Notice: Undefined index I want when I
Possible Duplicate: PHP: Notice: Undefined variable and Notice: Undefined index Okay, so I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.