UPDATE giving error but updates database

Asked

Viewed 100 times

3

inserir a descrição da imagem aqui

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_query inside mysqli_query, its variable $sql should be just a string; try this way.

  • @Rafaelacioly thus $sql = ($connects,"UPDATE agent SET login_on = '$date' WHERE login_ag = '$login' AND password = '$password'");

  • the clothesline $sql has to be just a string "UPDATE agent SET login_on = '$date' WHERE login_ag = '$login' AND password = '$password'";

  • @Rafaelacioly like this: --> $sql = "UPDATE agent SET login_on = '$date' WHERE login_ag = '$login' AND password = '$password'";

  • thanks! It worked out!

  • @Rafaelacioly writes the answer

  • I’ve already published the answer!

Show 2 more comments

2 answers

2

Seems to have a logic error in the block below:

    //primeira ocorrência
    $sql = mysqli_query($conecta,"UPDATE agente SET login_on = '$date' WHERE login_ag = '$login' AND senha_ag = '$senha'");
    if(mysqli_query($conecta,$sql)){//segunda ocorrência 
       echo "login_on modificado com sucesso!";
    }else{
       echo "erro ao relizar o login_on";
    }

The same query is executed twice and the second is already invalid because $sql is no longer a string but a resouce or false.

  • Thank you @rray :)

2


Your query is being executed one inside the other:

//primeira ocorrência
$sql = mysqli_query($conecta,"UPDATE agente SET login_on = '$date' WHERE login_ag = '$login' AND senha_ag = '$senha'");

//segunda ocorrência
if(mysqli_query($conecta,$sql)){
   echo "login_on modificado com sucesso!";
}else{
   echo "erro ao relizar o login_on";
}

Declare the clothesline $sql just like a normal string:

$sql = "UPDATE agente SET login_on = '$date' WHERE login_ag = '$login' AND senha_ag = '$senha'"

Then use her normally:

if(mysqli_query($conecta,$sql)){
 echo "login_on modificado com sucesso!";
}else{
 echo "erro ao relizar o login_on";
}
  • vlw @Rafaelacioly :)

Browser other questions tagged

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