duan19911992 2016-11-19 08:19
浏览 92

MYSQL PDO更新datetime什么都不做[重复]

This question already has an answer here:

Tried for an hour already. Help pls. Basically i want to update lastlogin column(MYSQL datetime) every time a user login. There is no error message and no update is happening. What is going on?

db.php

<?php 



    // These variables define the connection information for your MySQL database 

    $username = "root"; 

    $password = "root"; 

    $host = "localhost"; 

    $dbname = "rofl"; 



    $options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'); 

    try { $db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options); } 

    catch(PDOException $ex){ die("Failed to connect to the database: " . $ex->getMessage());} 

    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

    $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); 

    header('Content-Type: text/html; charset=utf-8'); 

    session_start();        

?>

main.php

<?php 
    include("DB/db.php"); 


        if(!empty($_POST)){   
            $query = " 
                SELECT 
                    userID, 
                    password, 
                    salt, 
                    email 
                FROM user 
                WHERE 
                    userID = :username 
            "; 
            $query_params = array( 
                ':username' => $_POST['useridforlogin'] 
            ); 

            try{ 
                $stmt = $db->prepare($query); 
                $result = $stmt->execute($query_params); 
            } 
            catch(PDOException $ex){ die("Failed to run query: " . $ex->getMessage()); } 
            $login_ok = false; 
            $row = $stmt->fetch();

            if($row){ 
                $check_password = hash('sha256', $_POST['passwordforlogin'] . $row['salt']); 
                for($round = 0; $round < 65536; $round++){
                    $check_password = hash('sha256', $check_password . $row['salt']);
                } 
                if($check_password === $row['password']){
                    $login_ok = true;
                } 
            } 
            if($login_ok){ 
                unset($row['salt']); 
                unset($row['password']); 
                $_SESSION['user'] = $row;  

             date_default_timezone_set('Asia/xxxxxx');
    $timestamp = date('Y/m/d h:i:s a', time()); 
    $query2 = "UPDATE user SET lastlogin= ':lastlogin' where userID =  ':username'";
            $query_params2 = array(  
                ':lastlogin' => $timestamp,
               ':username' => $_POST['useridforlogin']
               ); 
              try {  
                $stmt2 = $db->prepare($query2); 
                $result2 = $stmt2->execute($query_params2); 
            }  catch(PDOException $ex){ die("Failed to run query: " . $ex->getMessage()); }     
</div>
  • 写回答

1条回答 默认 最新

  • dongzhuo3376 2016-11-19 08:30
    关注

    I think the problem comes from your date format.

    Try replacing 'Y/m/d h:i:s a' by 'Y/m/d h:i:s' (removed the a)

    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)