doubo1871 2011-11-10 19:21
浏览 67
已采纳

如何使用jQuery将日期插入mysql表?

I am using a datetimepicker from jQuery, and when I select a date from the datetimepicker it puts it in this format:

  11/10/2011 14:02

Now when I try to insert it into my sql table like this

INSERT INTO MYTABLE (date, name) values ('$_POST[datepicker]','$_POST[name]')

it says the date is not in the correct format.

Can someone please help me determine how to format such date so that it is accepted by the datetime() field ?

Thank you

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '14:33:00,1,2,'Y')' at line 2

        $datetosave = date("Y-m-d H:i:s", strtotime($_POST[date]));

$insert_q = "INSERT INTO reservations (res_date, res_numofseats, user_id, rsvp)
                     VALUES ($datetosave,$_POST[cars],2,'Y')";
  • 写回答

4条回答 默认 最新

  • duanduo3712 2011-11-10 19:22
    关注

    Use the YYYY-MM-DD HH:MM:SS format, eg: 2011-11-10 14:02.

    Ideally, you'd want to set up your jQuery plugin to output in the correct format. If that's not possible, you can convert it to the correct format by parsing the date using PHP's strtotime() and date() functions:

    echo date('Y-m-d H:i:s', strtotime('11/10/2011 14:02')); 
    // prints 2011-11-10 14:02:00
    

    Reference: MySQL Manual on DATETIME

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?
  • ¥50 invest生境质量模块