1

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.

6
  • What's the error returned by mysql_query? Commented Jun 8, 2012 at 13:55
  • there is no error returned only a blank white screen. Commented Jun 8, 2012 at 13:55
  • where is the $date in the second query? Commented Jun 8, 2012 at 13:55
  • echo your query with the values instead of variables and share Commented Jun 8, 2012 at 13:57
  • i took it out in the second query and it works fine. that was to make sure that the date was the issue. Commented Jun 8, 2012 at 13:58

4 Answers 4

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.

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. UGGGGGGG I can't believe i missed something that simple.
np, more eyes helps sometimes (:
you should set display_errors to on in your php.ini, or use ini_set('display_errors', 'on); if you don't have access to it, or only need it temporarily. Also, error_reporting(E_ALL) if you don't already have it (:
@Steven: you could save precious time just by configuring your PHP environment (see comment above).
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.

Comments

0

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.