Page 1 of 1

How to add NOW() for DATETIME in $strQuery = INSERT INTO..?

Posted: Mon Oct 17, 2011 7:57 am
by mainsailcom
I need to capture the DATETIME field from an sql table and have gone blank on how to add the NOW() value to my insert code. Any help much appreciated Code is as follows:

Code: Select all

/* build the insert query */
$strQuery = "INSERT INTO table_name ".
			" ( q1, q2, q3, q4, q5, q6, q7, q8, q9, q10, q11, q12, q13, q14, q15, q16, q17, q18, q19,dt ) ".
			" VALUES ".
			" ( '$frmq1', ".
				" '$frmq2', ".
				" '$frmq3', ".
				" '$frmq4', ".
				" '$frmq5', ".
				" '$frmq6', ".
				" '$frmq7', ".
				" '$frmq8', ".
				" '$frmq9', ".
				" '$frmq10', ".
				" '$frmq11', ".
				" '$frmq12', ".
				" '$frmq13', ".
				" '$frmq14', ".
				" '$frmq15', ".
				" '$frmq16', ".
				" '$frmq17', ".
				" '$frmq18', ".
				" '$frmq19') ";

/* catch results */
$res = mysql_query($strQuery, $dbc)
	or die("<hr>Query failed : <br>[".mysql_errno($dbc)."] ".mysql_error($dbc)."<hr>\n");

if($res === true) {
	echo "Thank you for your participation. ".mysql_affected_rows($dbc). " record(s) inserted.";
}

/* clean up */
mysql_close($dbc);
?>

Re: How to add NOW() for DATETIME in $strQuery = INSERT INTO

Posted: Mon Oct 17, 2011 9:54 am
by ouchiko
??

INSERT INTO DB.Table SET DateField=NOW(), value='x', value2='y'

On the assumption DateField is set to datetime

Re: How to add NOW() for DATETIME in $strQuery = INSERT INTO

Posted: Mon Oct 17, 2011 10:24 am
by mainsailcom
Sorry still not clear on this. In the sql table, the field name = "dt", type = DATETIME, default = 0000-00-00 00:00:00
How do I add NOW() to the php VALUES list after " '$frmq19') "; ?

Re: How to add NOW() for DATETIME in $strQuery = INSERT INTO

Posted: Mon Oct 17, 2011 5:31 pm
by ouchiko
Just add

,NOW()

MySQL has an inbuilt function to create the datetime, no php needed.

Re: How to add NOW() for DATETIME in $strQuery = INSERT INTO

Posted: Tue Oct 18, 2011 4:38 pm
by mainsailcom
DateTime returns as default values (0000-00-00 00:00:00) so obviously not adding .NOW() correctly..Correction please:

$strQuery = "INSERT INTO bod1011 ".
" ( q1, q2, q3, q4, q5, q6, q7, q8, q9, q10, q11, q12, q13, q14, q15, q16, q17, q18, q19, dt ) ".
" VALUES ".
" ( '$frmq1', ".
" '$frmq2', ".
" '$frmq3', ".
" '$frmq4', ".
" '$frmq5', ".
" '$frmq6', ".
" '$frmq7', ".
" '$frmq8', ".
" '$frmq9', ".
" '$frmq10', ".
" '$frmq11', ".
" '$frmq12', ".
" '$frmq13', ".
" '$frmq14', ".
" '$frmq15', ".
" '$frmq16', ".
" '$frmq17', ".
" '$frmq18', ".
" '$frmq19' ,".
" '.NOW()' )";

Re: How to add NOW() for DATETIME in $strQuery = INSERT INTO

Posted: Tue Oct 18, 2011 4:43 pm
by twinedev
You are not using the function NOW(), you are using the string '.NOW()'

-Greg

Re: How to add NOW() for DATETIME in $strQuery = INSERT INTO

Posted: Tue Oct 18, 2011 6:50 pm
by mainsailcom
OK, how do I fix it?

Re: How to add NOW() for DATETIME in $strQuery = INSERT INTO

Posted: Tue Oct 18, 2011 7:02 pm
by Celauran

Code: Select all

$strQuery = "INSERT INTO bod1011 (q1, q2, q3, q4, q5, q6, q7, q8, q9, q10, q11, q12, q13, q14, q15, q16, q17, q18, q19, dt)
VALUES ('$frmq1', '$frmq2', '$frmq3', '$frmq4', '$frmq5', '$frmq6', '$frmq7', '$frmq8', '$frmq9', '$frmq10', '$frmq11', '$frmq12', '$frmq13', '$frmq14', '$frmq15', '$frmq16', '$frmq17', '$frmq18', '$frmq19', NOW())";

Re: How to add NOW() for DATETIME in $strQuery = INSERT INTO

Posted: Tue Oct 18, 2011 8:31 pm
by twinedev
mainsailcom wrote:OK, how do I fix it?
Replace '.NOW()' in your code with NOW()

Re: How to add NOW() for DATETIME in $strQuery = INSERT INTO

Posted: Wed Oct 19, 2011 7:05 am
by mainsailcom
Thank You Celauran, that did it...Much appreciated