具有插入日期的php / Mysql查询失败 [英] php/Mysql query with inserting date fails

查看:122
本文介绍了具有插入日期的php / Mysql查询失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一段时间,我无法弄清楚为什么我的查询不适用于日期。

I have been searching for quite a while now and I can't figure out why my query isn't working for dates.

我希望它被导入到我的数据库作为日期,这是从较高的一个选择,所以这里我是。

I want it to be imported to my database as a Date, this is a choice from higher up so here I am.

$insert_query = "INSERT INTO enrties(
`datum` )
VALUES (
'".mysql_real_escape_string($_SESSION['Datum'])."')

上面的几页我使用这段代码填写$ _SESSION

a few pages before the above i use this piece of code to fill the $_SESSION

$DateDag = $_POST['Dag'];
$DateMaand = $_POST['Maand'];
$DateJaar = $_POST['Jaar'];
switch ($dateMaand)
{
case 'Januari': $DateMaand = '01'; break;
case 'Februari': $DateMaand = '02'; break;
case 'Maart': $DateMaand = '03'; break; 
case 'April': $DateMaand = '04'; break; 
case 'Mei': $DateMaand = '05'; break;   
case 'Juni': $DateMaand = '06'; break;  
case 'July': $DateMaand = '07'; break;  
case 'Augustus': $DateMaand = '08'; break;  
case 'September': $DateMaand = '09'; break; 
case 'Oktober': $DateMaand = '10'; break;   
case 'November': $DateMaand = '11'; break;  
case 'December': $DateMaand = '12'; break;      
}
$_SESSION['Datum'] = $DateDag. '-'. $DateMaand. '-'. $DateJaar;



< 。

I want to have the date imported as a SQL Date format (preferably DD-MM-YYYY format).

解决方案:在交换机中出现错误,不明确,但它的工作原理;)

SOLVED, there was a mistake in the switch Apparantly, not really sure what but it works ;)

推荐答案

MySQL日期格式实际上是YYYY-MM-DD,所以如果要插入日期(猜测你的基准字段是Date或Datetime),你必须设置你的字符串

The MySQL date format is actually YYYY-MM-DD, so if you want to insert a date (guessing your datum field is Date or Datetime) you have to set up your string in that way.

如果你想以不同的方式插入日期,你应该使用这个:

If you want to insert your date in a different way you should use this:

INSERT INTO enrties (datum) VALUES (str_to_date('01/02/2000', '%d/%m/%Y')); 

但这将永远保存YYYY-MM-DD方式的日期,因为默认为mysql 。

but this will save always the date in YYYY-MM-DD way, since is the default for mysql.

这篇关于具有插入日期的php / Mysql查询失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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