Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/177275-solved-datetime-and-checkbox/
Share on other sites

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)

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'];

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

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!

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)
?>

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)
?>

$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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.