【发布时间】:2016-07-18 16:02:09
【问题描述】:
在我的 php 代码中,我在我的数据库中插入了一些数据。其中之一是日期。我想以 27-08-2016 的格式显示,但我返回 01-01-1970
这是我的代码。
<?php
include("./init.php");
if(isset($_POST['submit'])){
$post_game = $_POST['game'];
$time = strtotime($_POST['date']);
$post_date = date("d-m-Y", strtotime($time));
if($post_game==''){
echo "<script>alert('Please fill in all fields')</script>";
exit();
}
else {
$insert_game = "insert into last_game (game,date) values ('$post_game','$post_date')";
$run_posts = mysqli_query($con,$insert_game);
echo "<script>alert('Post Has been Published!')</script>";
echo "<script>window.open('index.php?last_game_details','_self')
</script>";
}
}
?>
关键是:
$post_date = date("d-m-Y", strtotime($time));
而我的php版本是:5.6.23
【问题讨论】:
-
strtotime 失败,返回布尔值 false,将类型转换为整数 0,即 1970 年 1 月 1 日。你很容易受到sql injection attacks 的攻击,你所有的代码都只是假设没有任何事情会失败,现在你正在为此受苦。永远不要永远假设成功。始终假设失败,检查失败,并将成功视为惊喜。
-
旁注,修复SQL注入漏洞。
-
除此之外,为什么要将
date作为非date对象插入数据库,日期格式应为Y-m-d以插入数据库 -
警告:使用
mysqli时,您应该使用parameterized queries 和bind_param将用户数据添加到您的查询中。 请勿使用字符串插值或连接来完成此操作,因为您创建了严重的SQL injection bug。 切勿将$_POST或$_GET数据直接放入查询中,如果有人试图利用您的错误,这可能会非常有害。