duanmei1850 2012-11-29 01:03
浏览 47
已采纳

下拉菜单中的php验证不正确

I have a code below where it displays students details in a drop down menu, and then it does a php validation to see if a student is selected from the drop down menu or not:

$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";
    }

}

The problem I have though is that even though I have selected a student from the drop down menu, it still displays a message stating that "You Must select a Student".

What is suppose to happen is that if the user has not selected a student or in other words when they have submitted the form and the drop down menu is still on the "Please Select" option, then it displays the message stating that user must select a student, else if the user does select a student from the drop down menu then display the message that a student has been selected.

  • 写回答

1条回答 默认 最新

  • douhao8456 2012-11-29 01:08
    关注

    You should use if($studentdrop == ""){ for equally or if($studentdrop != ""){ for not equally. Be aware on comparison operator.

    If you use if($studentdrop = ""){, it means that you assign empty string to variable $studentdrop.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c