使用 PHP Mysql 从表单插入日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12729113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:06:18  来源:igfitidea点击:

Inserting Date from form using PHP Mysql

phpmysql

提问by Steven

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>";
?>

回答by newfurniturey

I'm only guessing, but your Datefield in the database is either of type dateor datetime. The format of these are YYYY-mm-dd. Try changing your code to:

我只是猜测,但您Date在数据库中的字段是 typedatedatetime. 这些的格式是YYYY-mm-dd. 尝试将您的代码更改为:

$insertdate = date('Y-m-d', strtotime($_POST['Date']));

Also, you aren't converting the date to a timestamp before formatting it. I've added strtotime()on the data here as well.

此外,您不会在格式化之前将日期转换为时间戳。我也在strtotime()这里添加了数据。

回答by Zulakis

PHP's dateneeds a timestamp as second parameter. If $insertdateisn't one it won't work. You can though get a timestamp from a string using PHP's strtotime()function.

PHPdate需要一个时间戳作为第二个参数。如果$insertdate不是,它将无法工作。您可以使用 PHP 的strtotime()函数从字符串中获取时间戳。

Also, the value in your sql statement must be formatted according to the type used in your mysql database, for example date, datetimeor timestamp

此外,您的 sql 语句中的值必须根据您的 mysql 数据库中使用的类型进行格式化,例如datedatetimetimestamp

回答by james_tookey

You can use the NOW() MySQL function to insert the current date/time into the database. You'll just need to be using the Date, DateTime or Timestamp field types for this to work.

您可以使用 NOW() MySQL 函数将当前日期/时间插入到数据库中。您只需要使用 Date、DateTime 或 Timestamp 字段类型即可。

回答by Steven

I got it $insertdate = trim($_POST['Date']);

我知道了 $insertdate = trim($_POST['Date']);

$insertdate = mysql_real_escape_string($insertdate);
$insertdate = strtotime($insertdate);

    $insertdate = date('Y-m-d', $insertdate);

The strtotime fixed it but I had to rearrange the date format to Y-m-d

strtotime 修复了它,但我不得不将日期格式重新排列为 Ymd

Thank you for the help

感谢您的帮助