【问题标题】:error message on change password code using php [duplicate]使用php更改密码代码的错误消息[重复]
【发布时间】:2015-12-24 00:26:47
【问题描述】:

我使用脚本让我的用户在登录时更改他们的密码,当我测试它时,我显示了这个错误消息

注意:未定义索引:用户名在 /home/www/sitename/directory/changepw.php 在第 8 行 注意:未定义 索引:第 9 行的 /home/www/sitename/directory/changepw.php 中的 pin 注意:未定义索引:newpassword in /home/www/sitename/directory/changepw.php 第 10 行 注意:未定义 索引:/home/www/sitename/directory/changepw.php 中的重复新密码 在第 11 行

这就是我在第 8、9、10 和 11 行的内容

 $username = $_POST['username']; 
 $pin = $_POST['pin']; 
 $newpassword = $_POST['newpassword']; 
 $repeatnewpassword = $_POST['repeatnewpassword'];

下面是我的 HTML 代码

<style type="text/css">
    a:link {
       text-decoration: none;
    }
    a:visited {
        text-decoration: none;
    }
    a:hover {
        text-decoration: none;
    }
    a:active {
        text-decoration: none;
    }
</style>
<div id="inlogscherm">
<form name="form1" method="post" action="changepw.php">
    <div class="textm">Change password</div><br>
    <div class="text">Username:</div><div class="invulbalkje"><? echo    "{$_SESSION['username']}"; ?></div><br />
    <input name="username" type="text" id="username" value="<? echo   "{$_SESSION['username']}"; ?>">
  <div class="text">Password:</div><input name="pin" type="password"  id="pin" class="invulbalkje"><br />
    <div class="text">New Password:</div><input name="newpassword" type="password" id="newpassword" class="invulbalkje"><br />
    <div class="text">Repeat New Password:</div><input name="repeatnewpassword" type="password" id="repeatnewpassword" class="invulbalkje"><br />
    <input type="submit" name="Submit" value="Change" class="button">
  <a href="logout.php">LOGOUT</a>
</form>

下面是我的php代码

<?php 
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    session_start();
    include 'db.php';


    $username = $_POST['username'];
    $pin = $_POST['pin'];
    $newpassword = $_POST['newpassword'];
    $repeatnewpassword = $_POST['repeatnewpassword'];

    $encrypted_password=md5($pin);
    $encrypted_newpassword=md5($newpassword);
    $wtbusers = '`wtbusers`';//this should be defined (change this to your whatever table name)

    $result = mysql_query("SELECT pin FROM $wtbusers WHERE     username='$username' and pin = '$pin'");
    if(!$result) 
    { 
        echo"<script>alert('Please Fill Form Correctly')</script>"; } 
        if(mysql_num_rows($result) != 0){
            if($newpassword == $repeatnewpassword){
            $sql=mysql_query("UPDATE $wtbusers SET pin='$pin' where  username='$username'");        
            if($sql) 
            { 
                echo"<script>alert('Successful')</script>";
            }
            else
            {

                echo"<script>alert('error')</script>";
            }       
        } else {

          echo"<script>alert('error_password_not_matched')</script>";
    }
} else {

    echo"<script>alert('Please Fill Form Correctly')</script>";
}

?> 

谢谢。

【问题讨论】:

  • 您的 HTML 在哪里?这些错误很明显
  • 很明显您没有发送任何 POST 数据
  • 你的代码是SQL注入、XSS等的邀请
  • @Michelem 请告诉我如何发送帖子。谢谢
  • @Tina 是 changepw.php 文件上的 php 代码吗?并且您在尝试将值分配给用户名和密码时遇到错误。

标签: php error-code


【解决方案1】:

抛开所有其他问题并解决您的实际问题;没有检查是否正在制作我可以判断的 POST,因此在您的上下文中添加 @ 符号,例如 $username = @$_POST['username']; 是摆脱它们的最快解决方法,但我建议先检查帖子. isset 非常适合。

或者,您可以通过这样做关闭通知的错误报告

error_reporting(E_ALL & ~E_NOTICE);

error_reporting

【讨论】:

    【解决方案2】:

    这不是在 PHP 中使用会话将值分配给文本字段的方式。它有语法错误:

    <input name="username" type="text" id="username" value="<? echo "{$_SESSION['username']}"; ?>">
    

    我希望您在代码的开头(在您使用会话的每个文件中)都写了session_start();,否则所有这些(与会话相关)将毫无用处。

    试试这个:

    <?php
     session_start();
     $_SESSION['username'] = "mk";
     ?>
      <input name="username" type="text" id="username" value="<?php echo $_SESSION['username'];?>">
    

    现在,您必须以某种方式使用isset,这样如果不是来自表单,它就不会运行 php。

    <?php 
        error_reporting(E_ALL);
        ini_set('display_errors', 1);
        session_start();
        include 'db.php'; // you might want to make corresponding changes here
    
    if(isset($_POST['Submit'])){
        $username = $_POST['username'];
        $pin = $_POST['pin'];
        $newpassword = $_POST['newpassword'];
        $repeatnewpassword = $_POST['repeatnewpassword'];
    
        $encrypted_password=md5($pin);
        $encrypted_newpassword=md5($newpassword);
      // the rest of your code
    
       }
    ?>
    

    编辑:

    从 cmets 开始,这是简化/小错误修复代码:

    <?php 
        error_reporting(E_ALL);
        ini_set('display_errors', 1);
        session_start();
        include 'db.php';
    
    
    $servername = "localhost";
    $username = "your_user_name_here";
    $password = "your_pass_here";
    $dbname = "your_db_name_here";
    
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    
    
    if(isset($_POST['Submit'])){
        $username = $_POST['username'];
        $pin = $_POST['pin'];
        $newpassword = $_POST['newpassword'];
        $repeatnewpassword = $_POST['repeatnewpassword'];
    
        $encrypted_password=md5($pin);
        $encrypted_newpassword=md5($newpassword);
    
       $wtbusers = '`your_table_name_here`';       //this should be defined (change this to your whatever table name)
    
        $result = mysqli_query($conn, "SELECT pin FROM $wtbusers WHERE username= '$username' and pin = '$pin'");
        if(!$result) 
    
            echo"<script>alert('Please Fill Form Correctly')</script>";
    
            if(mysqli_num_rows($result) > 0){
                if($newpassword == $repeatnewpassword){
                $sql=mysqli_query($conn, "UPDATE $wtbusers SET pin= '$pin' WHERE username='$username'");        
                if($sql) 
                    echo"<script>alert('Successful')</script>";
                else
                    echo"<script>alert('error')</script>";
            } 
            else 
              echo"<script>alert('error_password_not_matched')</script>";
    } 
    else
        echo"<script>alert('Please Fill Form Correctly')</script>";
    
    ?> 
    

    【讨论】:

    • 谢谢一百万,我有一个问题,它成功了,但我的数据库没有用最近的密码更新
    • @Tina 你遇到了什么错误?开始$sql=mysql_query(); 这是错误的。避免使用 deprecated mysql 函数并移至mysqliPDO
    • @HawasKaPujaari 它不再显示任何错误消息它只是说成功后没有更新我的数据库
    • 我如何避免使用已弃用的 mysql 函数,因为我已经习惯了它。谢谢
    • @Tina 你的意思是即使在成功执行if($sql) 之后它也不会在数据库中显示更新的记录?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-28
    • 2017-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多