0

I am trying to insert date& time but error shows

SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect time value: '1505572990' for column 'vdate' at row 1

Please help me out by editing the code and getting solved the problem

My codes:

class.php

<?php
require_once 'db.php';
require_once 'lang.php';

class USER
{   

    private $conn;

    public function __construct()
    {
        $database = new Database();
        $db = $database->dbConnection();
        $this->conn = $db;
    }

    public function runQuery($sql)
    {
        $stmt = $this->conn->prepare($sql);
        return $stmt;
    }

    public function lasdID()
    {
        $stmt = $this->conn->lastInsertId();
        return $stmt;
    }

    public function register($uname)
    {
        try
        {                           

            $stmt = $this->conn->prepare("INSERT INTO validation(vdate) 
                                                         VALUES(:user_name)");
            $stmt->bindparam(":user_name",$uname);
            $stmt->execute();   
            return $stmt;
        }
        catch(PDOException $ex)
        {
            echo $ex->getMessage();
        }
    }
}

index.php

<?php
session_start();
require_once 'class.php';
$reg_user = new USER();

if(isset($_POST['btn-signup']))
{
    $uname = time();


        if($reg_user->register($uname))
        {           
            $msg = "
                    <div class='alert alert-success'>
                        <button class='close' data-dismiss='alert'>&times;</button>
                        <strong>Success!</strong> To activate your account, you need to verify your email. We have sent a verify link at <strong></strong>.
                    </div>
                    ";
        }

        else
        {
            echo "sorry , Query could no execute...";
        }       
    }
?>

The only thing I want that when a user clicks a button it should insert date&time in MySQL.

I tried the above code but it showed me error

SQLSTATE[22007]: Invalid DateTime format: 1292 Incorrect time value: '1505572990' for column 'vdate' at row 1

Please help me out by editing the code and getting solved the problem

Qirel
  • 25,449
  • 7
  • 45
  • 62
  • 1
    `1505572990` is a timestamp. What kind of type is the column you're inserting it to? – Qirel Sep 16 '17 at 15:44
  • @Qirel I am inserting it in `vdate` having type **datetime** –  Sep 16 '17 at 15:45
  • Either format it to a datestring or make the column of type timestamp. – Qirel Sep 16 '17 at 15:47
  • @Qirel how can I format it to `datestring`? –  Sep 16 '17 at 15:48
  • Consider naming your variables what they are, this will be confusing for future developers. If you are trying to insert the current datetime just use `now()`. – chris85 Sep 16 '17 at 15:49
  • to addition to chris85's comment.. just use `$stmt = $this->conn->prepare("INSERT INTO validation(vdate) VALUES(NOW())");` – Raymond Nijland Sep 16 '17 at 15:51
  • @chris85 I edited `$uname = now();` in index.php; it showed me the error `Fatal error: Call to undefined function now() `; Please edit my codes and answer it, it will help me lot. –  Sep 16 '17 at 15:52
  • now() is a MySQL function @Coder look mine comment. – Raymond Nijland Sep 16 '17 at 15:53
  • It goes in the query, not PHP, and don't bind it because it is a mysql function. @RaymondNijland Has it right, `(NOW())`. – chris85 Sep 16 '17 at 15:54
  • @RaymondNijland & chris85 Thanks for helping me –  Sep 16 '17 at 15:57
  • @chris85 Thank you for the help it solved my problem –  Sep 16 '17 at 15:59
  • @chris85 can you please help me with https://stackoverflow.com/questions/46261013/echo-some-text-if-time-is-more-than-time-stored-in-myysql –  Sep 17 '17 at 05:49

1 Answers1

0

If the type of the column 'vdate' is DATETIME, then you need to convert it before inserting

<?php $vdate = date("Y-m-d H:i:s", time()) ?>
leealex
  • 1,473
  • 1
  • 17
  • 24