3
I’m creating a page of login where I need to save to the Mysql database the date and time when the login user. In the database I have a table like this:
____________________________________________________
|  nome  | login  |  senha   |       login_on      | 
----------------------------------------------------
| Sarah  | Sarah@ | Sarah123 | 22/07/2016 16:30:24 |
----------------------------------------------------
When I make a login error message is displayed on the screen as shown in the image, but when I enter the database and give a refresh the updated table with the field is shown login_on filled. I don’t understand, someone can tell me why it performs UPDATE and shows error message?
There goes the code:
<?php //Página de login de agentes
include("conection.php");
if((isset($_POST['login']))&&(isset($_POST['senha']))){
    session_start();
    date_default_timezone_set('America/Sao_Paulo');
    $date = date("d-m-Y H:i:s");
    $login = $_POST['login'];
    $senha = $_POST['senha'];
    $query = mysqli_query($conecta,"SELECT login_ag, senha_ag FROM agente WHERE login_ag LIKE '$login' AND senha_ag LIKE '$senha'");
    $rows = mysqli_num_rows($query);
    if($rows > 0){
        $_SESSION['login'] = $login;
        $_SESSION['senha'] = $senha;
        $sql = mysqli_query($conecta,"UPDATE agente SET login_on = '$date' WHERE login_ag = '$login' AND senha_ag = '$senha'");
        if(mysqli_query($conecta,$sql)){
            echo "login_on modificado com sucesso!";
        }else{ echo "erro ao relizar o login_on";}
        //header('location:paginadeagente.php');    
        //echo $login.' '.$senha;
        echo $date;
    }else{
        unset ($_SESSION['login']);//<--destruindo variável
        unset ($_SESSION['senha']);//<--destruindo variável
        echo "<script>alert('Usuário ou senha incorretos!');</script>";
        //header('location:login.php');
    }
    //$End = microtime(true);
    //$Final = number_format(($End-$Start),6);
    //echo "<br><br>"."tempo gasto: $Final";
}
mysqli_close($conecta);
?>

you’re using
mysqli_queryinsidemysqli_query, its variable$sqlshould be just a string; try this way.– RFL
@Rafaelacioly thus $sql = ($connects,"UPDATE agent SET login_on = '$date' WHERE login_ag = '$login' AND password = '$password'");
– Sarah
the clothesline $sql has to be just a string "UPDATE agent SET login_on = '$date' WHERE login_ag = '$login' AND password = '$password'";
– RFL
@Rafaelacioly like this: --> $sql = "UPDATE agent SET login_on = '$date' WHERE login_ag = '$login' AND password = '$password'";
– Sarah
thanks! It worked out!
– Sarah
@Rafaelacioly writes the answer
– Sarah
I’ve already published the answer!
– RFL