2012-11-29 77 views
0

我有一個代碼下面,它顯示在下拉菜單中的學生的詳細信息,然後它進行php驗證,看看是否從下拉菜單中選擇一個學生: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

您應該同等使用if($studentdrop == ""){或不等於if($studentdrop != ""){。瞭解比較運營商。

如果您使用if($studentdrop = ""){,則表示您將空字符串分配給變量$studentdrop