Questions about Notice Undefined variable

Asked

Viewed 1,010 times

1

what this warning means Notice: Undefined variable: login in C: xampp htdocs Techphp profile.php on line 7 detail the login exist

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); 

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."; 
} 

This way we will be able to know if the login really exists and we will also be able to know if there is any mysql error in the query.

Based on this, we will know where to fix the error, if there is still.

  • Yes still an error does not access the login but has data in it , may be database error

  • what mistake you made?

  • Undefined variable: login in C: xampp htdocs Techphp profile.php on line 7 This shows the text saying that the login is empty, but has the login Cod in the database

  • modify as André suggested..

0

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

To run the <form> must 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 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 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);
}

Browser other questions tagged

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