使用PHP Mysql从表单插入日期 [英] Inserting Date from form using PHP Mysql
本文介绍了使用PHP Mysql从表单插入日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我似乎无法获得从表单插入数据库的日期.请帮忙!
I can not seem to get the date to insert from my form into my data base. Please help!
这是我的表格
here is my form
echo '
<h1>Enter New Report</h1>
<form action="submitreport.php" method="post"
enctype="multipart/form-data">
Date: <input type="text" name="Date" value='.$myDate.' /><br />
Report:<br><TEXTAREA NAME=Report ROWS=4 COLS=40></TEXTAREA><br />
<input type="submit" name="submit" value="Submit" />
</form>
';
?>
这是我写的要提交到数据库的内容
Here is what I have written to submit it to the database
$insertdate = trim($_POST['Date']);
$insertdate = mysql_real_escape_string($insertdate);
$insertdate = date('m/d/Y', $insertdate);
echo $insertdate;
mysql_query("INSERT INTO `Reports`(`Date`, `Report`) VALUES ('$insertdate','$_POST[Report]')") or die("load1 -" . mysql_error());
mysql_close();
echo "Thank You!" . " -" . "<a href='index.php' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
?>
推荐答案
我只是在猜测,但是您数据库中的Date
字段是date
或datetime
类型.这些文件的格式为YYYY-mm-dd
.尝试将代码更改为:
I'm only guessing, but your Date
field in the database is either of type date
or datetime
. The format of these are YYYY-mm-dd
. Try changing your code to:
$insertdate = date('Y-m-d', strtotime($_POST['Date']));
此外,您没有在格式化日期之前将日期转换为时间戳.我也在这里的数据上添加了strtotime()
.
Also, you aren't converting the date to a timestamp before formatting it. I've added strtotime()
on the data here as well.
这篇关于使用PHP Mysql从表单插入日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文