为了删除未定义的变量,下面的逻辑应该是什么?

时间:2012-12-09 02:07:33

标签: mysqli php

  

可能重复:
  PHP: “Notice: Undefined variable” and “Notice: Undefined index”

下面我有一段代码,其中包含一个包含2个文本输入的表单和一个包含从查询中检索的课程列表的下拉菜单。

我正在尝试使用下面的这行代码来确定数据是否成功,这样如果它不成功($numrowsstmt != 1),那么它会在所选的下拉菜单中保留所选的选项。如果成功,则返回Please Select选项

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

我遇到的问题是我在上面的行中为$numrowsstmt获取了一个未定义的变量,我得到这个的原因是因为该变量稍后在代码中被调用。

我的问题是,如何对代码/逻辑进行混洗并使其看起来如此,以便删除未定义的变量错误但是能够执行它想要做的事情?如果以下代码可以改组,那么我可以更改所有页面中的逻辑,以便能够遵循答案中的逻辑:

<?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>

2 个答案:

答案 0 :(得分:0)

isset($_POST['registerbtn'])检查之上,将$numrowsstmt设置为null或其他一些假值。

$courseHTML的构造移至以下 isset($_POST['registerbtn'])支票,上方构建$form

这样,变量就在范围内,并且有可能实际拥有一个值。你怎么看待这个呢?

答案 1 :(得分:0)

isset函数是您想要的,或者可能是empty

e.g

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