简体   繁体   English

DATETIME另存为0000-00-00 00:00:00

[英]DATETIME saving as 0000-00-00 00:00:00

This is my sql code ive ran this same code a good amount of times and never had any issues with it but today or some reason it is. 这是我的sql代码ive运行了相同的代码很多次,并且从未遇到过任何问题,但是今天还是某些原因。

mysqli_query($con, "
    INSERT INTO 'u_visits'
        ('ip_adress','dates')
    VALUES
        ('$ip',now())
    ON DUPLICATE KEY UPDATE visits = visits + 1
");
mysqli_close($con);

Everything works fine except my dates row gets added as 0000-00-00 00:00:00 一切正常,除了我的日期行被添加为0000-00-00 00:00:00

here is how i get the ip adress: 这是我如何获得IP地址的方法:

//Test if it is a shared client
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
    $ip = $_SERVER['HTTP_CLIENT_IP'];
    //Is it a proxy address
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
    $ip = $_SERVER['REMOTE_ADDR'];
}

$ip = ip2long($ip);

How about something like: 怎么样:

INSERT INTO `u_visits` (`ip_address`,`dates`) 
VALUES (
        '$ip', CONCAT(CURDATE(), ' ', CURTIME() )
       )
       ON DUPLICATE KEY UPDATE visits = visits +1

You should look into bound parameters for correct SQL injection prevention, as you are wide open as it stands. 您应该对绑定参数进行检查,以防止SQL注入正确,因为您已敞开大门。

Personally, I think your query is failing because your use of string dilemas ' instead of assigning table names/column names as a table using backticks (`). 就个人而言,我认为您的查询失败了,因为您使用了字符串dilemas '而不是使用反引号(`)将表名/列名分配为表。 So MySQL might be trying to iterate the necessary names as strings instead of table names/columns 因此,MySQL可能正在尝试将必要的名称迭代为字符串,而不是表名称/列

Try explicitly SETting: 尝试显式设置:

  INSERT INTO 'u_visits' SET ip_adress = '$ip', dates=NOW() ON ...

Also, as someone else mentioned, be absolutely sure $ip has been properly checked, otherwise you're subjecting yourself to SQL injection attacks. 另外,正如其他人提到的那样,请绝对确保已正确检查$ ip,否则您将遭受SQL注入攻击。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM