Asaf
Asaf

Reputation: 2035

PDO - Insertion (INSERT INTO...) code doesn't work

I'm trying to insert the current date/time to the database as the last time the user logged in. For some reason this doesn't work.
Other insertion scripts running on the very same page work well, and the connection is ok.
I use the same syntax to insert other stuff throughout the whole site, so I don't get what's wrong.
I can guarantee that the else part is being executed when I'm entering my password.

Here's the piece of code I am talking about.

if(!$pwVer){ 
     // code to execute if the password is incorrect.
} else {

    $dateT = date("Y-m-d H:i:s");

    $up_date = $con->prepare("INSERT INTO tbl_user_test (user_last_login) VALUES (:l_login)");
    $up_date->bindParam(':l_login', $dateT);

    $up_date->execute();

    validateUser($userid); //sets the session data for this user


    header("Location: cart.php");
    $con = null;
    die();
}

Some background:

Thanks in advance!

Upvotes: 0

Views: 1615

Answers (3)

Berry Langerak
Berry Langerak

Reputation: 18859

I'm trying to insert the current date/time to the database as the last time the user logged in. For some reason this doesn't work.

There is no error visible in your code, so it should be executed. One possible problem could be that PDO is in silent mode, which doesn't tell you what or if an error has occured. Set PDO to fail with an exception.

Also, there's no need to construct the current date yourself; you can use SQL's NOW().

<?php
$con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
try {
    $up_date = $con->prepare("INSERT INTO tbl_user_test (user_last_login) VALUES (NOW());");
    $up_date->execute();
}
catch( Exception $e ) {
    echo $e; exit;
}

header("Location: cart.php");
$con = null;
die();

Upvotes: 3

mrsrinivas
mrsrinivas

Reputation: 35444

Once check with this

$up_date->bindParam(':l_login', $dateT, PDO::PARAM_STR);

Upvotes: 2

Cl&#233;ment Andraud
Cl&#233;ment Andraud

Reputation: 9279

Did you try NOW() for the current date time ?

Upvotes: 2

Related Questions