Doubts about Notice Undefined variable [duplicate]

1

What this notice means Notice: Undefined variable: login in C: \ xampp \ htdocs \ Techphp \ perfil.php on line 7 detail login exists

if(isset($_POST['login'])){ 
$login = $_POST['login']; 
} else { 
echo "Login Vazio."; 
} 

$sql = mysql_query( "SELECT * FROM usuario WHERE login='{$login}' "); 
$linha = mysql_fetch_array($sql); 
    
asked by anonymous 15.08.2015 / 03:34

2 answers

0

I would do it this way:

if(isset($_POST['login'])){ 
    $login = $_POST['login'];
    $sql = mysql_query( "SELECT * FROM usuario WHERE login='{$login}' ") or print mysql_error(); 
    $linha = mysql_fetch_array($sql);  
} else { 
    echo "Login Vazio."; 
} 

In this way we will know if the login actually exists and we will also know if there is a mysql error in the query.

Based on this, we will know where to fix the error, if it still exists.

    
15.08.2015 / 03:45
0

The error occurs because you are probably sending a form without method="POST" or the name="" attribute in input is wrong.

To work with <form> should be something like:

<form method="POST" action="perfil.php">
   <input type="text" name="login" value="">
   <input type="submit" value="Buscar">
</form>

However by your query I suspect that you are not using a form but rather a GET method, probably a link with <a href="perfil.php?login=usuario">

Links do not use POST method, they use GET method, so the correct one is this:

$login = NULL; //Declaramos a variavel

if(false === empty($_GET['login'])){//Se existir define em login e não for vazia
    $login = $_GET['login'];
} else {//Se não existir ou for vazia emite um erro
    echo "Login Vazio."; 
}

if ($login) {//Se a variável existir executa a query
    $sql = mysql_query( "SELECT * FROM usuario WHERE login='{$login}'") or print mysql_error(); 
    $linha = mysql_fetch_array($sql);
}
    
15.08.2015 / 14:53