Steven
Steven

Reputation: 61

PHP / MYSQL Date will not insert

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

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

so I am not sure why it can not insert this into the database.

Upvotes: 1

Views: 2871

Answers (4)

wroniasty
wroniasty

Reputation: 8052

<?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.

Upvotes: 1

Ruben Nagoga
Ruben Nagoga

Reputation: 2218

Try to use new \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());

Upvotes: 0

Jim Rubenstein
Jim Rubenstein

Reputation: 6920

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.

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.

Upvotes: 4

Jimesh Gajera
Jimesh Gajera

Reputation: 612

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());

Upvotes: -1

Related Questions