草庐IT

php - 下拉菜单返回显示 "Please Select"并且未出现失败/成功消息

coder 2023-10-25 原文

我的以下代码有两个问题。

 <?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 中对此进行了测试。

最佳答案

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

希望对你有帮助

关于php - 下拉菜单返回显示 "Please Select"并且未出现失败/成功消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13617652/

有关php - 下拉菜单返回显示 "Please Select"并且未出现失败/成功消息的更多相关文章

  1. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  2. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  3. ruby-on-rails - Rails 编辑表单不显示嵌套项 - 2

    我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib

  4. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  5. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  6. ruby-on-rails - 使用 Sublime Text 3 突出显示 HTML 背景语法中的 ERB? - 2

    所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择

  7. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

    我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

  8. ruby-on-rails - link_to 不显示任何 rails - 2

    我试图在索引页中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的index.html.erb代码。ListingarticlesTitleTextssss我检查了我的路线,我认为它们也没有问题。PrefixVerbURIPatternController#Actionwelcome_indexGET/welcome/index(.:format)welcome#indexarticlesGET/articles(.:format)articles#indexPOST/articles(.:format)articles#createnew_articleGET/article

  9. ruby-on-rails - 相关表上的范围为 "WHERE ... LIKE" - 2

    我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que

  10. ruby - 即使失败也继续进行多主机测试 - 2

    我已经构建了一些serverspec代码来在多个主机上运行一组测试。问题是当任何测试失败时,测试会在当前主机停止。即使测试失败,我也希望它继续在所有主机上运行。Rakefile:namespace:specdotask:all=>hosts.map{|h|'spec:'+h.split('.')[0]}hosts.eachdo|host|begindesc"Runserverspecto#{host}"RSpec::Core::RakeTask.new(host)do|t|ENV['TARGET_HOST']=hostt.pattern="spec/cfengine3/*_spec.r

随机推荐