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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mainsailcom
Forum Newbie
Posts: 8
Joined: Sun Oct 09, 2011 11:09 am

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

Post 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);
?>
ouchiko
Forum Commoner
Posts: 35
Joined: Sun Oct 09, 2011 6:54 pm
Location: London

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

Post by ouchiko »

??

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

On the assumption DateField is set to datetime
mainsailcom
Forum Newbie
Posts: 8
Joined: Sun Oct 09, 2011 11:09 am

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

Post 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') "; ?
ouchiko
Forum Commoner
Posts: 35
Joined: Sun Oct 09, 2011 6:54 pm
Location: London

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

Post by ouchiko »

Just add

,NOW()

MySQL has an inbuilt function to create the datetime, no php needed.
mainsailcom
Forum Newbie
Posts: 8
Joined: Sun Oct 09, 2011 11:09 am

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

Post 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()' )";
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

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

Post by twinedev »

You are not using the function NOW(), you are using the string '.NOW()'

-Greg
mainsailcom
Forum Newbie
Posts: 8
Joined: Sun Oct 09, 2011 11:09 am

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

Post by mainsailcom »

OK, how do I fix it?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post 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())";
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

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

Post by twinedev »

mainsailcom wrote:OK, how do I fix it?
Replace '.NOW()' in your code with NOW()
mainsailcom
Forum Newbie
Posts: 8
Joined: Sun Oct 09, 2011 11:09 am

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

Post by mainsailcom »

Thank You Celauran, that did it...Much appreciated
Post Reply