Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
377 views
in Technique[技术] by (71.8m points)

how to insert datetime in mysql using php

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

code for the form-

           <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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...