3

下面的代码有两个问题。

 <?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 = "<span style='color: green'>Student " . $student . " - " . $firstname . " ". $surname . " has been Registered</span>";,为什么会出现这种情况?我知道 UPDATE 语句是正确的,因为我在 phpmyadmin 中对此进行了测试。

4

1 回答 1

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.

希望有帮助

于 2012-11-29T03:27:57.023 回答