2011-11-10 19:21
浏览 58


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')";

图片转代码服务由CSDN问答提供 功能建议


  11/10/2011 14:02 

现在当我尝试将其插入我的sql表时 像这样

  INSERT INTO MYTABLE(日期,名称)值('$ _POST [datepicker]','$ _ POST [name]')


有人可以帮我确定如何格式化这样的日期,以便日期时间接受它 ()字段?


 您的SQL语法有错误; 查看与您的MySQL服务器版本对应的手册,以便在'14:33:00,1,2,'Y')'附近使用正确的语法在第2行
 $ datetosave = date(“Ymd H:i  :s“,strtotime($ _ POST [date])); 
 $ insert_q =”INSERT INTO reservation(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

    点赞 打赏 评论
  • duanqiao9541 2011-11-10 19:32

    Use PHP's date function to convert it to MySQL's datetime format (YYYY-MM-DD HH:I:S)

    $mytime = date('Y-m-d H:i:s', strtotime('11/10/2011 14:02'));

    点赞 打赏 评论
  • doumi1884 2011-11-10 20:16

    I believe the problem is lack of quotes around the inserted date:

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

    gives you VALUES 2011-11-10 14:33:00,1,2,'Y'

    Add ticks around $datetosave

    $insert_q = "INSERT INTO reservations (res_date, res_numofseats, user_id, rsvp)
    VALUES ('".$datetosave."',$_POST[cars],2,'Y')";
    点赞 打赏 评论
  • doujunchi1238 2011-11-10 21:33

    This may be out of the scope of your question, but your code is vulnerable to SQL Injection attacks. Try to always use inbuilt escaping/quoting methods of your database library or use the prepared statements. Not sure what library are you using, so few links here (I would paste more but SE does not allow me to do so :) ):

    点赞 打赏 评论

相关推荐 更多相似问题