atrocious Posted October 11, 2009 Share Posted October 11, 2009 Hi Everyone, I am trying to figure out how to insert the current date and time into the database. Scenario: 1) user enter my site and fills up the forum 2) hits the submit button 3) Regardless of the user location I want the time to be stored in the database in PST. Question: Do I need to get an input from the user for time? or can I just use some server time etc? Right now I'm asking them to manually insert the date and time in the textbox which is not that effective Also I have a checkbox in my forums but I always get 0 in the value field when I check the database. I have it setup as (tinyint(1)) in the database using mysql. I think I am doing wrong. Is there a way to require the user to check the button before they hit the "Submit" button? Thank you in advance for the help. Quote Link to comment https://forums.phpfreaks.com/topic/177275-solved-datetime-and-checkbox/ Share on other sites More sharing options...
atrocious Posted October 11, 2009 Author Share Posted October 11, 2009 Sorry I'm asking so much question in one thread. I want to record the IP-address of the users who submit the form. How do I do that? Quote Link to comment https://forums.phpfreaks.com/topic/177275-solved-datetime-and-checkbox/#findComment-934699 Share on other sites More sharing options...
avvllvva Posted October 11, 2009 Share Posted October 11, 2009 For the current date-time u can try the Mysql function NOW(), and make the field type into "DATETIME". For checkbox validation use some javascript, and give some default value to it so that u can insert that value in db if (!document.form.checkboxname.checked) Quote Link to comment https://forums.phpfreaks.com/topic/177275-solved-datetime-and-checkbox/#findComment-934720 Share on other sites More sharing options...
mikesta707 Posted October 11, 2009 Share Posted October 11, 2009 you can use PHP's url=http://us3.php.net/manual/en/function.date.php]date[/url] function also to get time. You can use javascript to validate the checkbox is checked before you let the user submit, but you want to validate it with PHP also, since the user can turn javascript off. also, to get a users IP, use the following $ip=$_SERVER['REMOTE_ADDR']; Quote Link to comment https://forums.phpfreaks.com/topic/177275-solved-datetime-and-checkbox/#findComment-934723 Share on other sites More sharing options...
Zane Posted October 11, 2009 Share Posted October 11, 2009 You need to look at the $_SERVER superglobal it's an array that has all this information (that you don't need to ask people for).... here's an example http://zanedockery.com/temp/server.php If you look at these keys Inside the $_SERVER superglobal Array ( [REMOTE_ADDR] => 72.71.118.60 [...] [sCRIPT_NAME] => /temp/server.php [sCRIPT_URL] => /temp/server.php [DOCUMENT_URI] => /temp/server.php [TZ] => EST5EDT [REQUEST_TIME] => 1255239774 } you'll find the Time Zone and the IP of the user/viewer Quote Link to comment https://forums.phpfreaks.com/topic/177275-solved-datetime-and-checkbox/#findComment-934727 Share on other sites More sharing options...
atrocious Posted October 11, 2009 Author Share Posted October 11, 2009 Thank you so much. I will look into it now. Quote Link to comment https://forums.phpfreaks.com/topic/177275-solved-datetime-and-checkbox/#findComment-934765 Share on other sites More sharing options...
atrocious Posted October 14, 2009 Author Share Posted October 14, 2009 you can use PHP's url=http://us3.php.net/manual/en/function.date.php]date[/url] function also to get time. You can use javascript to validate the checkbox is checked before you let the user submit, but you want to validate it with PHP also, since the user can turn javascript off. also, to get a users IP, use the following $ip=$_SERVER['REMOTE_ADDR']; Thank you. It worked and I'm able to successfully add the users IP to the database and read it as well by populating it to the table. Still trying to figure out how to insert the data and time when user hits the submit button! Quote Link to comment https://forums.phpfreaks.com/topic/177275-solved-datetime-and-checkbox/#findComment-936501 Share on other sites More sharing options...
atrocious Posted October 14, 2009 Author Share Posted October 14, 2009 You need to look at the $_SERVER superglobal it's an array that has all this information (that you don't need to ask people for).... here's an example http://zanedockery.com/temp/server.php If you look at these keys Inside the $_SERVER superglobal Array ( [REMOTE_ADDR] => 72.71.118.60 [...] [sCRIPT_NAME] => /temp/server.php [sCRIPT_URL] => /temp/server.php [DOCUMENT_URI] => /temp/server.php [TZ] => EST5EDT [REQUEST_TIME] => 1255239774 } you'll find the Time Zone and the IP of the user/viewer Below is the code I'm using to inject data into the database. Tell me what code should I add into my existing code so that I can inject the data/time to my database. <?php $con = mysql_connect ( "sql.com" , "app" , "pw" ) or die(mysql_error()); mysql_select_db( "newapp" ) or die(mysql_error()); $sql="INSERT INTO tablename (name, address, age, email, comment,ip) VALUES('$_POST[name]','$_POST[address]','$_POST[age]','$_POST[email]','$_POST[comment]','$_SERVER[REMOTE_ADDR]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } mysql_close($con) ?> Quote Link to comment https://forums.phpfreaks.com/topic/177275-solved-datetime-and-checkbox/#findComment-936503 Share on other sites More sharing options...
mikesta707 Posted October 14, 2009 Share Posted October 14, 2009 check out date_default_timezone_set(). probably want to use that in conjunction with data() to do what you want Quote Link to comment https://forums.phpfreaks.com/topic/177275-solved-datetime-and-checkbox/#findComment-936508 Share on other sites More sharing options...
atrocious Posted October 14, 2009 Author Share Posted October 14, 2009 I tried doing this. Didn't work! lol Correct me. <?php $con = mysql_connect ( "host" , "database1" , "pw" ) or die(mysql_error()); mysql_select_db( "data" ) or die(mysql_error()); $sql="INSERT INTO test11 (name, ip, date) VALUES('$_POST[name]','$_SERVER[REMOTE_ADDR]','(date_default_timezone_get())')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } mysql_close($con) ?> Quote Link to comment https://forums.phpfreaks.com/topic/177275-solved-datetime-and-checkbox/#findComment-936517 Share on other sites More sharing options...
atrocious Posted October 14, 2009 Author Share Posted October 14, 2009 anyone? Quote Link to comment https://forums.phpfreaks.com/topic/177275-solved-datetime-and-checkbox/#findComment-936595 Share on other sites More sharing options...
avvllvva Posted October 14, 2009 Share Posted October 14, 2009 $sql="INSERT INTO test11 (name, ip, date) VALUES('$_POST[name]','$_SERVER[REMOTE_ADDR]',NOW())"; Quote Link to comment https://forums.phpfreaks.com/topic/177275-solved-datetime-and-checkbox/#findComment-936611 Share on other sites More sharing options...
atrocious Posted October 14, 2009 Author Share Posted October 14, 2009 $sql="INSERT INTO test11 (name, ip, date) VALUES('$_POST[name]','$_SERVER[REMOTE_ADDR]',NOW())"; Great! It works. Thank you so much!!!!!!!!! but how do i convert the time to PST. Lets say a user from EST injects to the database. I want to be able to see the time in PST. Quote Link to comment https://forums.phpfreaks.com/topic/177275-solved-datetime-and-checkbox/#findComment-936613 Share on other sites More sharing options...
atrocious Posted October 14, 2009 Author Share Posted October 14, 2009 bump Quote Link to comment https://forums.phpfreaks.com/topic/177275-solved-datetime-and-checkbox/#findComment-936888 Share on other sites More sharing options...
avvllvva Posted October 15, 2009 Share Posted October 15, 2009 suppose if your server is on PST, you haven't do anything, it will pick up server time by default. I think NOW() returns time on the server. Quote Link to comment https://forums.phpfreaks.com/topic/177275-solved-datetime-and-checkbox/#findComment-937216 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.