I am trying to insert a date from a variable into a mysql database. The format of the column is date and it has dates in the column. The dates in the column look like yyyy-mm-dd
我正在尝试将一个日期从一个变量插入到一个mysql数据库中。列的格式是日期,它在列中有日期。列中的日期看起来像yyyy-mm-dd。
my date variable also looks like this however it will not insert the date into the column and even i do not get an error just a white screen.
我的日期变量看起来也像这样,但是它不会将日期插入到列中,甚至我也不会在一个白色屏幕上出现错误。
<?php
//here is the code to insert this does not work
mysql_query("INSERT INTO `invoices` (account_id, purchased_date, sales_rep, INV_ID)
VALUES ('".$acctid."', '".$date"','".$row['8']."', '".$invid."' )") or die("load1 -" . mysql_error());
<?php
//this does work but it does not have the date.
mysql_query("INSERT INTO `invoices` (account_id, sales_rep, INV_ID)
VALUES ('".$acctid."', '".$row['8']."', '".$invid."')") or die("load1 -" . mysql_error());
not sure what the problem is. I have displayed the $date variable onto the screen and it looks fine ex. 2012-06-01
不知道是什么问题。我已经在屏幕上显示了$date变量,它看起来很好。2012-06-01。
so I am not sure why it can not insert this into the database.
所以我不确定为什么它不能把这个插入到数据库中。
4 个解决方案
#1
4
Your error is that you have a parse error in this line:
您的错误是在这一行有一个解析错误:
VALUES ('".$acctid."', '".$date"','".$row['8']."', '".$invid."' )")
Your server has display_errors
turned off, so you're not seeing the fatal error output.
您的服务器已经关闭了display_errors,因此您没有看到致命错误输出。
You can fix it by adding a concatenation operator (.
) like so:
您可以通过添加一个连接操作符(.)来修复它:
VALUES ('".$acctid."', '".$date."','".$row['8']."', '".$invid."' )")
Also, in the future, I find it more readable to write my queries like so:
而且,在将来,我发现写这样的查询更容易理解:
VALUES ('{$acctid}', '{$date}', '{$row['8']}', '{$invid}')
If you prefer not to use interpolation (that's the method of string "injection" used above), you could still use concatenation (your original method) but use spaces to make it more readable (and easier to find syntax errors before you try to execute it):
如果您不喜欢使用插值(这是上面使用的字符串“注入”的方法),您仍然可以使用连接(您的原始方法),但是使用空格使它更容易阅读(并且在您尝试执行它之前更容易找到语法错误):
"VALUES ('" . $acctid . "', '" . $date . "' , '" . $row['8'] . "', '" . $invid . "')";
And before all the haters shun me for suggesting interpolation over concatenation, let me refer you to this tweet by @rasmus stating that interpolation is actually faster than concatenation, these days.
在所有的仇恨者都不同意我提出插值的问题之前,让我用@rasmus来介绍这条推文,他说插值实际上要比连接速度快。
#2
1
<?php
//here is the code to insert this does not work
mysql_query("INSERT INTO `invoices` (account_id, purchased_date, sales_rep, INV_ID) VALUES ('".$acctid."', '".$date"','".$row['8']."', '".$invid."' )") or die("load1 -" . mysql_error());
?>
the error is:
错误的是:
PHP Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING on line 1
There is no .
after $date
.
没有。之后美元日期。
#3
0
Try to use new \DateTime('yyyy-mm-dd')
尝试使用新的\DateTime('yyyy-mm-dd')
<?php
//here is the code to insert this does not work
mysql_query("INSERT INTO `invoices` (account_id, purchased_date, sales_rep, INV_ID)
VALUES ('".$acctid."', '".new \DateTime('yyyy-mm-dd')."','".$row['8']."', '".$invid."' )") or die("load1 -" . mysql_error());
#4
-1
You can use
您可以使用
mysql_query("INSERT INTO `vipanda2`.`invoices` (account_id, purchased_date, sales_rep, INV_ID)
VALUES ('".$acctid."', '".date('Y-m-d',mktime(0, 0, 0, date("m", $date), date("d", $date), date("Y", $date)))."','".$row['8']."', '".$invid."' )") or die("load1 -" . mysql_error());
#1
4
Your error is that you have a parse error in this line:
您的错误是在这一行有一个解析错误:
VALUES ('".$acctid."', '".$date"','".$row['8']."', '".$invid."' )")
Your server has display_errors
turned off, so you're not seeing the fatal error output.
您的服务器已经关闭了display_errors,因此您没有看到致命错误输出。
You can fix it by adding a concatenation operator (.
) like so:
您可以通过添加一个连接操作符(.)来修复它:
VALUES ('".$acctid."', '".$date."','".$row['8']."', '".$invid."' )")
Also, in the future, I find it more readable to write my queries like so:
而且,在将来,我发现写这样的查询更容易理解:
VALUES ('{$acctid}', '{$date}', '{$row['8']}', '{$invid}')
If you prefer not to use interpolation (that's the method of string "injection" used above), you could still use concatenation (your original method) but use spaces to make it more readable (and easier to find syntax errors before you try to execute it):
如果您不喜欢使用插值(这是上面使用的字符串“注入”的方法),您仍然可以使用连接(您的原始方法),但是使用空格使它更容易阅读(并且在您尝试执行它之前更容易找到语法错误):
"VALUES ('" . $acctid . "', '" . $date . "' , '" . $row['8'] . "', '" . $invid . "')";
And before all the haters shun me for suggesting interpolation over concatenation, let me refer you to this tweet by @rasmus stating that interpolation is actually faster than concatenation, these days.
在所有的仇恨者都不同意我提出插值的问题之前,让我用@rasmus来介绍这条推文,他说插值实际上要比连接速度快。
#2
1
<?php
//here is the code to insert this does not work
mysql_query("INSERT INTO `invoices` (account_id, purchased_date, sales_rep, INV_ID) VALUES ('".$acctid."', '".$date"','".$row['8']."', '".$invid."' )") or die("load1 -" . mysql_error());
?>
the error is:
错误的是:
PHP Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING on line 1
There is no .
after $date
.
没有。之后美元日期。
#3
0
Try to use new \DateTime('yyyy-mm-dd')
尝试使用新的\DateTime('yyyy-mm-dd')
<?php
//here is the code to insert this does not work
mysql_query("INSERT INTO `invoices` (account_id, purchased_date, sales_rep, INV_ID)
VALUES ('".$acctid."', '".new \DateTime('yyyy-mm-dd')."','".$row['8']."', '".$invid."' )") or die("load1 -" . mysql_error());
#4
-1
You can use
您可以使用
mysql_query("INSERT INTO `vipanda2`.`invoices` (account_id, purchased_date, sales_rep, INV_ID)
VALUES ('".$acctid."', '".date('Y-m-d',mktime(0, 0, 0, date("m", $date), date("d", $date), date("Y", $date)))."','".$row['8']."', '".$invid."' )") or die("load1 -" . mysql_error());