Undefined index: submit

0

I have the following code and I get the following error

<?php$user=$_SESSION['MM_Username'];if($user){//userisloggedinif($_POST['submit']){//startchangingpassword$oldpassword=md5($_POST['senha_atual']);$newpassword=md5($_POST['senha_nova']);$repeatnewpassword=md5($_POST['confirme_senha']);//checkpasswordagainstdb//connectdbmysql_select_db($database_bvmarco,$bvmarco);$query_password=sprintf("SELECT password FROM login WHERE numMec = '$user'");
        $password = mysql_query($query_password, $bvmarco) or die(mysql_error());
        $row_password = mysql_fetch_assoc($password);
        $totalRows_password = mysql_num_rows($password);

        $oldpassworddb=$row_password['password'];
        //check password

        if($oldpassword==$oldpassworddb){
            //chek new passwords
            if($newpassword==$repeatnewpassword){
                //success
                //change password in db
                $querychange = mysql_query("UPDATE login SET password='$newpassword' WHERE numMec = '$user'");
                session_destroy();
                die("Password alterada com sucesso!");
            }else{
                die("As password's não são iguais");
            }
        }else{
            die("A password atual não bate certo");
        }
    }else{
        echo"<div class='card-body'>
            <form METHOD='POST' name='password'>
                <div class='align-items-center justify-content-center'>
                  <div class='form-group'>
                        <label><b>Antiga password</b></label>
                        <input type='text' placeholder='Antiga password' name='senha_atual' class='form-control'>
                    </div>
                    <div class='form-group'>
                        <label for='tipo'><b>Nova password</b></label>
                        <input type='text' placeholder='Nova password' name='senha_nova' class='form-control''>
                        <label for='tipo'><b>Confirme password</b></label>
                        <input type='text' placeholder='Nova password' name='confirme_senha' class='form-control'>
                    </div>
                    <input type='submit' name='submit' id='submt' value='Change password'>
                </div>
            </form> 
        </div>";
    }
}else{
    die("Precisas fazer o login para mudar a password!");
}
?>
    
asked by anonymous 19.12.2017 / 19:03

1 answer

1

This is not quite a bug, it's a Notice only, in production this should not even be displayed, read more at:

Still, it's best to use isset or !empty to check if the variables exist, for example:

<?php
if(!empty($_SESSION['MM_Username'])){
    $user=$_SESSION['MM_Username'];

    //user is logged in
    if(isset($_POST['submit'])){
        //start changing password
        $oldpassword=md5($_POST['senha_atual']);
        $newpassword=md5 ($_POST['senha_nova']);
        $repeatnewpassword=md5 ($_POST['confirme_senha']);          
        //check password against db
        //connect db
        mysql_select_db($database_bvmarco, $bvmarco);
        $query_password = sprintf("SELECT password FROM login WHERE numMec = '$user'");
        $password = mysql_query($query_password, $bvmarco) or die(mysql_error());
        $row_password = mysql_fetch_assoc($password);
        $totalRows_password = mysql_num_rows($password);

        $oldpassworddb=$row_password['password'];
        //check password

        if($oldpassword==$oldpassworddb){
            //chek new passwords
            if($newpassword==$repeatnewpassword){
                //success
                //change password in db
                $querychange = mysql_query("UPDATE login SET password='$newpassword' WHERE numMec = '$user'");
                session_destroy();
                die("Password alterada com sucesso!");
            }else{
                die("As password's não são iguais");
            }
        }else{
            die("A password atual não bate certo");
        }
    }else{
        echo"<div class='card-body'>
            <form METHOD='POST' name='password'>
                <div class='align-items-center justify-content-center'>
                  <div class='form-group'>
                        <label><b>Antiga password</b></label>
                        <input type='text' placeholder='Antiga password' name='senha_atual' class='form-control'>
                    </div>
                    <div class='form-group'>
                        <label for='tipo'><b>Nova password</b></label>
                        <input type='text' placeholder='Nova password' name='senha_nova' class='form-control''>
                        <label for='tipo'><b>Confirme password</b></label>
                        <input type='text' placeholder='Nova password' name='confirme_senha' class='form-control'>
                    </div>
                    <input type='submit' name='submit' id='submt' value='Change password'>
                </div>
            </form> 
        </div>";
    }
}else{
    die("Precisas fazer o login para mudar a password!");
}
?>

Very important: The old mysql API for PHP has been discontinued

All functions starting with mysql_ are part of the old PHP API, it is recommended that you switch to one of the new APIs:

  • PDO
  • MYSQLI

Read more at: MySQL vs PDO - Which is the most recommended to use?

    
19.12.2017 / 19:07