DYWBH Posted June 16, 2011 Share Posted June 16, 2011 Hello, I have created a very basic form that inserts user information to a MySql Table. I am now trying to make it insert the current date into a Date column within the table. Here is my code: <?php mysql_connect("mysql9.000webhost.com", "a3313201_admin", "xxxxxx") or die(mysql_error()); mysql_select_db("a3313201_admin") or die(mysql_error()); $Username = $_POST['Username']; $Password= $_POST['Password']; $query="INSERT INTO Users (Username, Password, [b]DateJoin[/b])VALUES('".$Username."', '".$Password."', [b]???[/b])"; mysql_query($query) or die ('Error 101'); echo "Accoount Created"; ?> Thank you for your help Quote Link to comment https://forums.phpfreaks.com/topic/239541-inserting-date-into-mysql-table-with-php/ Share on other sites More sharing options...
fugix Posted June 16, 2011 Share Posted June 16, 2011 you can achieve this by using the CURDATE() function $query="INSERT INTO Users (Username, Password, [b]DateJoin[/b])VALUES('$Username', '$Password', CURDATE())"; Quote Link to comment https://forums.phpfreaks.com/topic/239541-inserting-date-into-mysql-table-with-php/#findComment-1230475 Share on other sites More sharing options...
Skylight_lady Posted June 16, 2011 Share Posted June 16, 2011 <?php mysql_connect("mysql9.000webhost.com", "a3313201_admin", "xxxxxx") or die(mysql_error()); mysql_select_db("a3313201_admin") or die(mysql_error()); $Username = $_POST['Username']; $Password= $_POST['Password']; $DateJoin= $_POST['DateJoin']; $query="INSERT INTO Users (Username, Password, DateJoin) VALUES ('".$Username."', '".$Password."', '".$DateJoin."')"; mysql_query($query) or die ('Error 101'); echo "Accoount Created"; ?> While in the form where you $_POST the username and password add this as a hidden field: <input type="hidden" name="DateJoin" id="DateJoin" value="<?php echo date('Y-m-d H:i:s'); ?>" /> Now the date('Y-m-d H:i:s'); is to be used if you have the column DateJoin in the sql table set as a DateTime. fugix's solution is another way. Both ways work. Quote Link to comment https://forums.phpfreaks.com/topic/239541-inserting-date-into-mysql-table-with-php/#findComment-1230476 Share on other sites More sharing options...
DYWBH Posted June 16, 2011 Author Share Posted June 16, 2011 Thanks guys, got it working How do I mark thread as 'Solved'? Quote Link to comment https://forums.phpfreaks.com/topic/239541-inserting-date-into-mysql-table-with-php/#findComment-1230477 Share on other sites More sharing options...
fugix Posted June 16, 2011 Share Posted June 16, 2011 bottom left hand of this page, its a grayish button Quote Link to comment https://forums.phpfreaks.com/topic/239541-inserting-date-into-mysql-table-with-php/#findComment-1230481 Share on other sites More sharing options...
PFMaBiSmAd Posted June 16, 2011 Share Posted June 16, 2011 You wouldn't pass a server-side produced value through the form, because that would just add another piece of external modifiable data that you must validate and escape before putting it into the query statement. Quote Link to comment https://forums.phpfreaks.com/topic/239541-inserting-date-into-mysql-table-with-php/#findComment-1230482 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.