Error change password on login system with PHP SQL Server

Asked

Viewed 106 times

1

Good afternoon, I’m having a big problem where I can’t get the password in the system. What is bugging me is that it seems that sqlsrv_fetch_array works differently from mysql_fetch_array, I have a code like this using Mysql and works well but SQL Server is not, so I came here asking for help on this system.

public function alterarsenha($Nome, $Senha, $Novasenha, $Confirnovasenha){
        $sql = "SELECT SENHA FROM CADPES WHERE NOME = '$Nome'";
        $query = sqlsrv_query($this->Conn->Conectar(), $sql) or die( print_r( sqlsrv_errors(), true));  

        $senhabanco = '';
        while($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)){

            $senhabanco = $row['SENHA'];
        }            
            if(($Senhanova == "") OR ($Confirsenha) == "" ){
                echo "Insira a nova senha!";
                //header('location:index.php');
            }else{
                if($Senha != $senhabanco){
                    echo "Nome ou senha inválido!";
                }else{
                   if($Novasenha != $Confirnovasenha){
                    echo "Campos da nova senha não conferem!";
                }else{
                    $upd = "UPDATE CADPES SET SENHA = '$Novasenha' WHERE NOME = '$NOME'";
                    $upd_query = sqlsrv_query($this->Conn->Conectar(), $sql);

                        if($upd_query){
                            echo "Senha alterado com sucesso";
                            //header('location:index.php');
                        }else{
                            echo "Ocorreu um erro na trocra de Números";
                            //header('location:index.php');
                        }
                }
            }   
    }

I’m stuck here !

if($Senha != $senhabanco){
      echo "Nome ou senha inválido!";
}else{

The problem is that the $senhabanco variable always has the NULL value even though I did the while. And I’ve even tried to change the place key put at the end but results in another mistake.

  • Tried to print the values of the two variables with var_dump()/print_r()?

  • yes, inside while not printed anything, outside it prints null!

1 answer

0

After snooping for hours I found the 3 errors that caused the system not to run.

1º no while, 2º query had passed the wrong parameter and last in sqlsrv_query I had put before $sql.

public function alterarsenha($Nome, $Senha, $Novasenha, $Confirnovasenha){
        $sql = "SELECT SENHA FROM CADPES WHERE NOME = '$Nome'";
        $query = sqlsrv_query($this->Conn->Conectar(), $sql) or die( print_r( sqlsrv_errors(), true));  

    $senhabanco = '';
    while($row = sqlsrv_fetch_array($query)){

        $senhabanco = $row['SENHA'];
    }            
        if(($Senhanova == "") OR ($Confirsenha) == "" ){
            echo "Insira a nova senha!";
            //header('location:index.php');
        }else{
            if($Senha != $senhabanco){
                echo "Nome ou senha inválido!";
            }else{
               if($Novasenha != $Confirnovasenha){
                echo "Campos da nova senha não conferem!";
            }else{
                $upd = "UPDATE CADPES SET SENHA = '$Novasenha' WHERE NOME = '$Nome'";
                $upd_query = sqlsrv_query($this->Conn->Conectar(), $upd);

                    if($upd_query){
                        echo "Senha alterado com sucesso";
                        //header('location:index.php');
                    }else{
                        echo "Ocorreu um erro na trocra de Números";
                        //header('location:index.php');
                    }
            }
        }   
}
  • 1

    After snooping around for hours I found the three mistakes that kept the system from running. 1º no while, 2º query had passed the wrong parameter and last in sqlsrv_query I had put before $sql.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.