Trium918 Posted December 10, 2006 Share Posted December 10, 2006 Ok, I finally got the the user's register date into the datebase, but now I am trying to write a program that will calculate the user's last login by updating reg_date columm in the user table.This is just a simple html code that just enter password and username[code]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Basic Authentication Script</title></head><body bgcolor="#FFCOCB" text="#000000"><p> </p><p> </p><p> </p><form name="form1" method="post" action="last_visit.php"><table width="500" border="5" align="center" cellspacing="2" cellpadding="10"><tr bgcolor="#EEEEEE"><td height="1">Password<input type="password" name="password"/><br />Username<input type="text" name="user"/><input type="submit" name="Submit" value="Login" /></td></tr><tr><td bgcolor="#DDDDDD"><div align="center"><b>Enter the password to see the next page.</b></div></td></tr></table></body></html>[/code]This is the php code that checks enter the usename and time of registeration into the database[code=php:0]<? $host="localhost";$database="Date"; $dusername="root";$x=mysql_connect($host,$database,$dusername);$x1=mysql_select_db($database)or die("Could not select Date database !"); $time = time(); $query = "insert into user_data(reg_date,user)values ($time, '$user')";$result=mysql_query($query);if (!($result)) { die("User Register date could not be stored"); } if (empty($password)){die("You did not specify the password!");}if (!($password == "pass")){die("You did not specify the valid password.");}?>[/code] Link to comment https://forums.phpfreaks.com/topic/30088-solved-date-and-time/ Share on other sites More sharing options...
trq Posted December 10, 2006 Share Posted December 10, 2006 Simply create a filed in the database of type [i]timestamp[/i]. Then use mysql's NOW() function to insert a timestamp. Link to comment https://forums.phpfreaks.com/topic/30088-solved-date-and-time/#findComment-138309 Share on other sites More sharing options...
Drezard Posted December 10, 2006 Share Posted December 10, 2006 PHP Also has a time function called Date(); You can use this with a number of parameters. An example would be, if you want to get the Day, Month and Year. You would use Date("d/m/Y");You can check this function up on php.net or this link : http://www.w3schools.com/php/php_date.aspCheers, Daniel Link to comment https://forums.phpfreaks.com/topic/30088-solved-date-and-time/#findComment-138316 Share on other sites More sharing options...
Trium918 Posted December 10, 2006 Author Share Posted December 10, 2006 I'm aware of the Date() function but I have no idea where to start. I know how to enter data into a database ,but how do I enter the user register date in the database? Link to comment https://forums.phpfreaks.com/topic/30088-solved-date-and-time/#findComment-138318 Share on other sites More sharing options...
trq Posted December 10, 2006 Share Posted December 10, 2006 For starters, Date is a reserved word in mysql, so shouldn't be used as a database or field name.[code]<?php $host="localhost"; $database = "DateDB"; // Ive changed your db name. $dusername = "root" ; $dbpassword = ""; mysql_connect($host,$database,$dusername,$dpassword) ; mysql_select_db($database) or die( "Could not select DateDB database !" ) ; $query = "INSERT INTO DateDB useradded VALUES ('NOW()')"; // Ive changed your field name. if (mysql_query($query)) echo "success"; } else { echo mysql_error(); }?>[/code]PS: The board has its own syntax highlighting, just wrap all your code in either [ php ][ /php ] or [ code ][ /code ] tags without the spaces. Link to comment https://forums.phpfreaks.com/topic/30088-solved-date-and-time/#findComment-138321 Share on other sites More sharing options...
roopurt18 Posted December 10, 2006 Share Posted December 10, 2006 You should keep in mind that the server is not always in the same time zone as you and sometimes the mysql server is not the same machine as your apache server. For instance on the old web host we used at work, if I used date() from within PHP I got a time on the east coast of the United States. If I used NOW() in MySQL I got a time a few hours ahead of that.What I recommend is using a statement like:[code]SELECT DATE_FORMAT(NOW(), 'H:i:s') FROM table WHERE 1 LIMIT 1[/code]That will give you the time of the MySQL server. Let's say that time is 3 hours ahead of where you are, then you can add a line:[code=php:0]define('DB_NOW', '(NOW() - 3 * 60 * 60)');[/code]to your general DB.php file.Now whenever you need to insert the current DATETIME into a table you can just do:[code=php:0]$sql = "INSERT INTO table (fld1, fld2, the_date) VALUES (val1, 'val2', '" . DB_NOW . "')";[/code]If you ever move servers, you only need to adjust a single line of code to compensate for the new server's possible timezone difference than the old one. Link to comment https://forums.phpfreaks.com/topic/30088-solved-date-and-time/#findComment-138328 Share on other sites More sharing options...
Trium918 Posted December 10, 2006 Author Share Posted December 10, 2006 Ok, I tried added the Now() function but I am now getting this error:You have an error in your SQL syntax near 'user_date VALUES ('NOW()')' at line 1Error: Link to comment https://forums.phpfreaks.com/topic/30088-solved-date-and-time/#findComment-138330 Share on other sites More sharing options...
roopurt18 Posted December 10, 2006 Share Posted December 10, 2006 Oops, I made a mistake. Don't enclose it in single quotes. Link to comment https://forums.phpfreaks.com/topic/30088-solved-date-and-time/#findComment-138334 Share on other sites More sharing options...
Trium918 Posted December 11, 2006 Author Share Posted December 11, 2006 Thorpe, I do not know how to remove threads and when I use your script the Now() function is inserted in the database not the time. Link to comment https://forums.phpfreaks.com/topic/30088-solved-date-and-time/#findComment-138839 Share on other sites More sharing options...
trq Posted December 11, 2006 Share Posted December 11, 2006 Post your current code. Link to comment https://forums.phpfreaks.com/topic/30088-solved-date-and-time/#findComment-138880 Share on other sites More sharing options...
Drezard Posted December 11, 2006 Share Posted December 11, 2006 Just use the date() function its easier. You just have to use this code... [CODE]<?php// Using some of thorpes code... $host="localhost"; $database = "DateDB"; // Ive changed your db name. $dusername = "root" ; $dbpassword = ""; mysql_connect($host,$database,$dusername,$dpassword) ; mysql_select_db($database) or die( "Could not select DateDB database !" ) ; $date = Date("d/m/Y"); $query = "INSERT INTO DateDB useradded VALUES ('$date')"; // Ive changed your field name. if (mysql_query($query)) echo "success"; } else { echo mysql_error(); }?>[/CODE]Use that code.Thanks, Daniel Link to comment https://forums.phpfreaks.com/topic/30088-solved-date-and-time/#findComment-138886 Share on other sites More sharing options...
Trium918 Posted December 11, 2006 Author Share Posted December 11, 2006 Ok, this code will only allow me to enter the user register date once.Afterward, the code [code=php:0]die("User Register date could not be stored"); [/code]is displayed in the browser. Could someone please tell me what am I doing wrong,and how to fix it?[code=php:0]$host="localhost";$database="Date"; //The name of the database$dusername="root";//$dpassword="";,$dpassword$x=mysql_connect($host,$database,$dusername);$x1=mysql_select_db($database)or die("Could not select Date database !");$date = date('n/j/Y');$query="INSERT into user_data $reg_date Values('$date')";$result=mysql_query($query);if (!($result)) { die("User Register date could not be stored"); } [/code] Link to comment https://forums.phpfreaks.com/topic/30088-solved-date-and-time/#findComment-139143 Share on other sites More sharing options...
Trium918 Posted December 11, 2006 Author Share Posted December 11, 2006 Could someone please try the above code out for me to see if they can get it working? Link to comment https://forums.phpfreaks.com/topic/30088-solved-date-and-time/#findComment-139252 Share on other sites More sharing options...
timmah1 Posted December 11, 2006 Share Posted December 11, 2006 This is how I do it, and I've never had a problem on my site[code]$ip = $REMOTE_ADDR;$query="update users set last_login=NOW(),last_ip='$ip' WHERE username='$username'";[/code]Puts the last time the user logs in and what ip they logged in from Link to comment https://forums.phpfreaks.com/topic/30088-solved-date-and-time/#findComment-139257 Share on other sites More sharing options...
timmah1 Posted December 12, 2006 Share Posted December 12, 2006 I tried to reply to your personal mesage, but it says I am not allowed to send personal messages, go figure.Anyhow, to answer your question, to insert the date during registration, it would be the same as updating the Last Login:I'm just running a simple Photo hosting site, so my database is small, as well as my registration.Here is the sql for what my site uses:[code]CREATE TABLE `users` ( `user_id` int(11) NOT NULL auto_increment, `username` varchar(255) NOT NULL default '', `password` varchar(255) NOT NULL default '', `email` varchar(255) NOT NULL default '', `signup_date` datetime NOT NULL default '0000-00-00 00:00:00', `ip` varchar(255) NOT NULL default '', `last_login` timestamp NOT NULL default CURRENT_TIMESTAMP, `last_ip` varchar(255) NOT NULL default '', PRIMARY KEY (`user_id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;[/code]And then my script to insert the data[code]// connect to the mysql server$link = mysql_connect($server, $db_user, $db_pass)or die ("Could not connect to mysql because ".mysql_error());// select the databasemysql_select_db($database)or die ("Could not select database because ".mysql_error());// check if the username is taken$check = "select id from $table where username = '".$_POST['username']."';"; $qry = mysql_query($check)or die ("Could not match data because ".mysql_error());$num_rows = mysql_num_rows($qry); if ($num_rows != 0) { echo "Sorry there the username $username is already taken.<br>";echo "<a href=register1.php>Try again</a>";exit; } //check if email was used// check if the username is taken$check = "select id from $table where email = '".$_POST['email']."';"; $qry = mysql_query($check)or die ("Could not match data because ".mysql_error());$num_rows = mysql_num_rows($qry); if ($num_rows != 0) { echo "Sorry, that email has already been used ($email).<br>";echo "<a href=register1.php>Try again</a>";exit; }else {// insert the data$ip = $REMOTE_ADDR;$insert = mysql_query("insert into $table values ('NULL', '".$_POST['username']."', '".$_POST['password']."', '".$_POST['email']."',NOW(),'$ip',NOW(),'".$_POST['username']."')")or die("Could not insert data because ".mysql_error());//make directorymkdir("/your/direct path/$_POST[username]");$result = mkdir($_POST[username],0777); if(!$result) { echo "Error: Couldn't create the directory for $_POST[username]!<BR>"; } {copy ("/your/direct path/index.php", "/your/direct path/$_POST[username]/index.php") or die("Could not copy index file");}//mail the user $subject = "Account Confirmation!"; $message = " Hi $_POST[username] -- Thanks for joining!Here's your account info for logging in:E-mail: $_POST[username].Password: $_POST[password].Keep it secret. Keep it safe.===================================NOTE: This email is never sent unsolicited. If you believe you received this notification in error, please send an email to-------------------------<!-- __$email__ --> This is an automated response, please do not reply!"; mail($_POST[email], $subject, $message, "From: Welcome to My Website<register@website.com>\n");// print a success messageecho "Your user account has been created! for $username<br>"; echo "Now you can login to your new address: <a href=http://www.website.com/$_POST[username]>http://www.website.com/$_POST[username]</a>"; }[/code] Link to comment https://forums.phpfreaks.com/topic/30088-solved-date-and-time/#findComment-139598 Share on other sites More sharing options...
Trium918 Posted December 19, 2006 Author Share Posted December 19, 2006 Please read modified topic above. Thank you! Link to comment https://forums.phpfreaks.com/topic/30088-solved-date-and-time/#findComment-144708 Share on other sites More sharing options...
Recommended Posts