Y
Y
Yura Komarov2017-10-03 19:22:06
PHP
Yura Komarov, 2017-10-03 19:22:06

How to correctly write date to mysql table or what is wrong?

I am getting in js const date = Date.now(); and throw it through ajax in php code

<?php
require_once "db.inc.php";
function clearStr($data){
  global $link;
  return mysqli_real_escape_string($link, trim(strip_tags($data)));
}
function clearInt($data){
  return abs((int)$data);
}

function updateTask($task, $date){
  global $link;
  $status = 0;
  $sql = "INSERT INTO tasks (text, status, timetask) VALUES (?, ?, ?)";
  if ($stmt = mysqli_prepare($link, $sql)){

    mysqli_stmt_bind_param($stmt, "sii", $task, $status, $date);
    mysqli_stmt_execute($stmt);
    $rows = mysqli_stmt_affected_rows($stmt);
    return $rows;
  }
  return $stmt;
}
if ($_SERVER['REQUEST_METHOD'] == 'POST'){

  if (isset($_POST['add'])){
    $task = clearStr($_POST['task']);
    $date = $_POST['date'];
    $res = updateTask($task, $date);
    echo $_POST['date']; 

    // echo $res;
  }
}

and I expect that 1507046844096 will be written to my database,
but for some reason 2147483647 in the database in all cells of the table The
question is why?
timetask int(255) UNSIGNED (null) (Default /0) --- row in table (settings)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Z
zorca, 2017-10-03
@Yurajun

It would be necessary to choose a different type of field, got out of the INT limits:

INT or INTEGER - integers of ordinary capacity - range of signed numbers: -2147483648 ... 2147483647, unsigned - 0 ... 4294967295

Y
Yura Komarov, 2017-10-03
@Yurajun

Thank you. It's always like that, you think about one thing, but in reality it's another)))

D
Don Gan, 2017-10-04
@PravdorubMSK

1. For TIMESTAMP there is a corresponding field type - TIMESTAMP, that's what it's called.
2. Decide why you need to store in TIMESTAMP? https://habrahabr.ru/post/61391/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question