【发布时间】:2017-01-10 09:14:30
【问题描述】:
我的表单上的一个字段是日期,而使用谷歌浏览器时,单击该字段时会显示一个日历框。如果我选择一个日期,格式为 DD-MM-YYYY,但当它添加到我的表中时,它显示为 YYYY-MM-DD。
我猜我的代码中遗漏了一些东西,并且在插入时将其设置为默认格式? 这是我的插入代码:
if (isset($_POST['addloan'])):
$username=$_POST['username'];
$user_query="SELECT * from loanusers where username = '$username'"; //think about how you are storing usernames in db, in this query you have to enter the username exactly how it is stored
$userresult= mysqli_query($connection, $user_query);
$row = mysqli_fetch_array($userresult, MYSQLI_ASSOC);
$emailaddress = $row['emailaddress'];
$product=$_POST['product'];
$product_desc=$_POST['product_desc'];
$serial=$_POST['serial'];
$date_collected=$_POST['date_collected'];
$date_return=$_POST['date_return'];
$returned=$_POST['returned'];
if (isset($_FILES['loanform'])){
$exten = pathinfo($_FILES['loanform']['name'], PATHINFO_EXTENSION);
$loanform = $_FILES['loanform']['name'];
$uploadfile = "loanforms/$loanform";
if (strtolower($exten) == 'pdf'){
move_uploaded_file($_FILES['loanform']['tmp_name'] , $uploadfile);
$my_query="INSERT INTO loans VALUES ('','$username','$emailaddress','$product','$product_desc','$serial','$date_collected','$date_return','$loanform','$returned','')";
$result= mysqli_query($connection, $my_query);
} else{
header('Location: incorrectext.php');
}
}
if ($result):
header ('location: homepage.php?confirm=New loan successfully created');
else :
echo "<b>This didn`t work, error: </b>";
echo mysqli_error($connection);
endif;
endif;
【问题讨论】:
-
您的代码很容易受到 SQL 注入的影响——想象一下有人发布了用户名
'; DROP TABLE loanusers; --。请了解参数化查询以保护自己,并且不要忘记规则 #1:永远不要相信用户输入。另见:bobby tables -
数据库总是接受 YYYY-MM-DD 格式的日期,它不会接受 DD-MM-YYYY
-
不是php的问题。它是db中的日期格式。
-
尝试转换日期格式
date ("Y-m-d H:i:s", strtotime($date_return)); -
@Mucca019 是的,只有 chrome 支持。您必须使用其他浏览器的插件。