0

我在下面有一个代码,它在下拉菜单中显示学生详细信息,然后它执行 php 验证以查看是否从下拉菜单中选择了学生:

$sql = "SELECT StudentUsername, StudentForename, StudentSurname FROM Student ORDER BY StudentUsername"; 

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

$sqlstmt->execute(); 

$sqlstmt->bind_result($dbStudentUsername, $dbStudentForename, $dbStudentSurname);

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

$studentHTML = "";  
$studentHTML .= '<select name="students" id="studentsDrop">'.PHP_EOL; 
$studentHTML .= '<option value="">Please Select</option>'.PHP_EOL;  

$outputstudent = "";

while($sqlstmt->fetch()) 
{
    $student = $dbStudentUsername;
    $firstname = $dbStudentForename;
    $surname = $dbStudentSurname; 

    $studentHTML .= "<option value='" . $student . "'>" . $student . " - " . $firstname . " " . $surname . "</option>" . PHP_EOL;  
} 

$studentHTML .= '</select>'; 

$errormsg = (isset($errormsg)) ? $errormsg : '';

if(isset($_POST['resetpass'])) {
    //get the form data
    $studentdrop = (isset($_POST['students'])) ? $_POST['students'] : '';

    if($studentdrop = "") {
        $errormsg = "Student is Selected";
    }
    else{
        $errormsg = "You must Select a Student";
    }

}

我遇到的问题是,即使我从下拉菜单中选择了一个学生,它仍然会显示一条消息,指出“您必须选择一个学生”。

假设发生的情况是,如果用户没有选择学生,或者换句话说,当他们提交表单并且下拉菜单仍然在“请选择”选项上时,它会显示一条消息,说明用户必须选择学生,否则如果用户确实从下拉菜单中选择了学生,则显示学生已被选中的消息。

4

1 回答 1

4

您应该使用if($studentdrop == ""){for equal 或if($studentdrop != ""){for not equal。请注意比较运算符。

如果使用if($studentdrop = ""){,则意味着您将空字符串分配给变量$studentdrop

于 2012-11-29T01:08:02.993 回答