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

查看:24
本文介绍了插入日期的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;

我想将日期导入为 SQL 日期格式(最好是 DD-MM-YYYY 格式).

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天全站免登陆