如何使用php在mysql中插入日期时间 [英] how to insert datetime in mysql using php

查看:53
本文介绍了如何使用php在mysql中插入日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个应用程序.我创建了一个表单,用户可以在其中选择年、月、日、小时和分钟.我想知道如何将它插入到我的 sql 数据库中.谢谢

i am currently creating an application. I have created a form where the user is able to choose a year,month,day,hour and minute. I was wondering how to insert this into my sql database. Thanks

表格代码-

           <select name="day" id="day">
 <?php
for($i=1;$i<=31;$i++) 
{
echo "<option>" . $i . "</option>";
}
?>
</select>
<td>
<select name="month" id="month">

  <?php
for($i=1;$i<=12;$i++) 
{
echo "<option>" . $i . "</option>";
}
?>
</select>
<td>

<select name="year" id="year">
  <?php
for($i=2015;$i<=2016;$i++) 
{
echo "<option>" . $i . "</option>";
}
?>
  <td>
  </select>

    <select name="Hour" id="hour">
 <?php
for($i=0;$i<=23;$i++) 
{
echo "<option>" . sprintf("%02d",$i) . "</option>";
}
?>

</select>
<td>
<select name="minute">
<?php
for($i=0;$i<=60;$i++) 
{
echo "<option>" . sprintf("%02d",$i) . "</option>";
}
?>
</select>

推荐答案

您将其放入表单中(从此链接阅读所有内容),然后使用 $_POST['day'] 等并使用插入查询将其放入数据库中.我不会说太多,因为我根本不建议这样做.

You put this in a form(read everything from this link) and then use $_POST['day'] etc and use an insert query to put it in the database. I wont say too much about this since I wouldn't recommend doing it like this at all.

您正在检查一个月中的哪几天.如果它少于 31 天,则它是有效的.<这不是真的.如果日期少于 31 天,则并非所有日期都有效. 已经存在实现此目的的方法.这要容易得多.PHP 日期时间.您还可以使用 mysql 中的 datetime 类型验证大部分内容.我建议您通读所有链接.

You're checking the days of the month. If it has less than 31 days in it, it's valid. < This is not true. Not all the dates are valid if it has less than 31 days in it. There's ways to do this which already exist. It's so much easier. PHP datetime. You can also validate most with the datetime type in mysql. I'd recommend you to read through all of the links.

这篇关于如何使用php在mysql中插入日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆