【问题标题】:drop down menu goes back to displaying "Please Select" and fail/success message not appearing下拉菜单返回显示“请选择”并且未出现失败/成功消息
【发布时间】:2012-11-17 01:07:13
【问题描述】:

下面的代码有两个问题。

 <?php

$validSubmission = isset($_POST['resetpass']) && $_POST['students'] && $_POST['newpass'] && $_POST['confirmpass'];


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

    if (!$validSubmission && isset($_POST['students']) && $student == $_POST['students'])
    {
        $studentHTML .= "<option value='" . $student . "' selected='selected'>" . $student . " - " . $firstname . " " . $surname . "</option>" . PHP_EOL;
    }
    else
    {
        $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'] : '';
    $newpass     = (isset($_POST['newpass'])) ? $_POST['newpass'] : '';
    $confirmpass = (isset($_POST['confirmpass'])) ? $_POST['confirmpass'] : '';

    //make sure all data was entered
    if ($studentdrop != "")
    {
        if ($newpass)
        {
            if (strlen($newpass) <= 5)
            {
                $errormsg = "Your Password must be a minimum of 6 characters or more";
            }
            else
            {
                if ($confirmpass)
                {
                    if ($newpass === $confirmpass)
                    {
                        //Make sure password is correct
                        $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", $username);
                        // execute query
                        $stmt->execute();
                        // get result and assign variables (prefix with db)
                        $stmt->bind_result($dbStudentUsername);
                        //get number of rows
                        $stmt->store_result();
                        $numrows = $stmt->num_rows();

                        if ($numrows == 1)
                        {
                            //encrypt new password
                            $newpassword = md5(md5("93w" . $newpass . "ed0"));

                            //update the db

                            $updatesql = "UPDATE Student SET StudentPassword = ? WHERE StudentUsername = ?";
                            $update    = $mysqli->prepare($updatesql);
                            $update->bind_param("ss", $newpassword, $username);
                            $update->execute();

                            //make sure the password is changed

                            $query = "SELECT StudentUsername, StudentPassword FROM Student WHERE StudentUsername = ? AND StudentPassword = ?";
                            // prepare query
                            $stmt  = $mysqli->prepare($query);
                            // You only need to call bind_param once
                            $stmt->bind_param("ss", $username, $newpassword);
                            // execute query
                            $stmt->execute();
                            // get result and assign variables (prefix with db)
                            $stmt->bind_result($dbStudentUsername, $dbStudentPassword);
                            //get number of rows
                            $stmt->store_result();
                            $numrows = $stmt->num_rows();

                            if ($numrows == 1)
                            {
                                $errormsg = "<span style='color: green'>Student " . $student . " - " . $firstname . " " . $surname . " has been Registered</span>";

                            }
                            else
                            {
                                $errormsg = "An error has occured, the Password was not Reset";
                            }
                        }
                    }
                    else
                    {
                        $errormsg = "Your New Password did not Match";
                    }
                }
                else
                {
                    $errormsg = "You must Confirm your New Password";
                }
            }
        }
        else
        {
            $errormsg = "You must Enter your New Password";
        }

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

} 

我正在尝试创建一个管理员可以重置学生密码的休息密码页面。

问题 1:

在我的代码中,我想要做的是,如果出现 php 验证消息(除了显示成功消息的 $errormsg 之外,出现 $errormsg 之一),那么 students 下拉菜单应该仍然显示提交表单后选择的选项。现在,这适用于用户将文本输入留空的所有验证消息,但唯一不起作用的验证消息是用户没有为新密码和确认密码输入匹配的密码。如果出现$errormsg = "Your New Password did not Match"; ,则学生下拉菜单将返回Please Select 选项。为什么每次出现此验证消息时都会返回 Please Select 选项,如果发生此验证,我如何保持所选学生仍处于选中状态?

问题 2:

如果我成功输入所有详细信息并提交,它不会执行插入,但它不会显示失败消息$errormsg = "An error has occured, the Password was not Reset"; 或成功消息$errormsg = "&lt;span style='color: green'&gt;Student " . $student . " - " . $firstname . " ". $surname . " has been Registered&lt;/span&gt;";,为什么会这样?我知道 UPDATE 语句是正确的,因为我在 phpmyadmin 中对此进行了测试。

【问题讨论】:

    标签: php html mysql


    【解决方案1】:

    $username(第 72 行及以后)从未设置。我想这应该来自“$studentdrop”?

    这意味着你更新 where StudentUsername == '',这将失败。

    帮助您调试:

    1. Turn on warning and notices in the error handler for writing code ( error_reporting(E_ALL); ) as it will reveal problems like this
    2. As opposed to constantly counting the rows, you can save time in that the bind_result/store_value won't work unless you got a result. So you can check that value you get in bind_result - and if you had checked that `$dbStudentUsername == $username` in line 78, then it would have also thrown a wobbly at that stage.
    3. When you've done the "update", you can check the number of "affected rows"; if this > 0 then the password has been updated; no need for a secondary DB query.
    

    希望有帮助

    【讨论】:

    • 如果你不介意,我可以问你一个问题吗?罗比,我将所有$username 更改为$student,现在如果我选择学生用户名u0867587 的学生,当我填写表格时在提交后,在成功消息中显示学生u0753334已成功重置密码?你知道这是为什么吗?是不是因为$student 在while 循环中,所以只有while 循环中的任何东西都可以使用$student?如果是这样,那么我应该将其更改为什么或能够检索$student 变量?
    • $student 将是该获取循环中的最后一个 - 因此您将更改最后一个用户的密码,而不是选定的用户。据我所知,您需要做的就是将(原始)$username 引用更改为 $studentdrop(这是来自 POST 的值)以使其正常工作。
    猜你喜欢
    • 1970-01-01
    • 2012-11-11
    • 2016-09-27
    • 2020-02-21
    • 1970-01-01
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多