使用php更改密码代码的错误消息

时间:2015-09-26 07:07:05

标签: php error-code

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

  

注意:未定义的索引:用户名   第8行/home/www/sitename/directory/changepw.php注意:未定义   index:第9行/home/www/sitename/directory/changepw.php中的引脚   注意:未定义的索引:newpassword in   第10行/home/www/sitename/directory/changepw.php注意:未定义   index:/home/www/sitename/directory/changepw.php中的repeatnewpassword   在第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>";
}

?> 

谢谢。

2 个答案:

答案 0 :(得分:0)

抛开所有其他问题并解决您的实际问题;没有检查是否正在制作一个我可以告诉的POST,所以在你的上下文中添加一个@符号就像$username = @$_POST['username'];一样是摆脱它们的最快的解决方法,但我建议检查一下发帖先。 isset非常适合。

或者,您可以通过执行此操作来关闭通知的错误报告

error_reporting(E_ALL & ~E_NOTICE);

请参阅error_reporting

答案 1 :(得分:0)

这不是您如何使用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

   }
?>

修改

从评论中继续,这是简化/次要错误修复代码:

<?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>";

?>