Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Jak zapisać do tabeli ostatnie logowanie?
Forum PHP.pl > Forum > Bazy danych > MySQL
badziol1
Witam.
Jestem jeszcze zielony w temacie php+msql. I bardzo proszę o podpowiedź.
Chcę zapisać datę ostatniego logowania do kolumny lastlogin. Tu jest struktura mojej bazy oraz dołączam plik logowania.
  1. <?php require_once('Connections/myconn.php'); ?>
  2. <?php
  3. if (!function_exists("GetSQLValueString")) {
  4. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
  5. {
  6. if (PHP_VERSION < 6) {
  7. $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  8. }
  9.  
  10. $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  11.  
  12. switch ($theType) {
  13. case "text":
  14. $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  15. break;
  16. case "long":
  17. case "int":
  18. $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  19. break;
  20. case "double":
  21. $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  22. break;
  23. case "date":
  24. $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  25. break;
  26. case "defined":
  27. $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  28. break;
  29. }
  30. return $theValue;
  31. }
  32. }
  33. ?>
  34. <?php require_once('Connections/myconn.php'); ?>
  35. <?php
  36. if (!function_exists("GetSQLValueString")) {
  37. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
  38. {
  39. if (PHP_VERSION < 6) {
  40. $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  41. }
  42.  
  43. $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  44.  
  45. switch ($theType) {
  46. case "text":
  47. $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  48. break;
  49. case "long":
  50. case "int":
  51. $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  52. break;
  53. case "double":
  54. $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  55. break;
  56. case "date":
  57. $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  58. break;
  59. case "defined":
  60. $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  61. break;
  62. }
  63. return $theValue;
  64. }
  65. }
  66. ?>
  67. <?php
  68. // *** Validate request to login to this site.
  69. if (!isset($_SESSION)) {
  70. }
  71.  
  72. $loginFormAction = $_SERVER['PHP_SELF'];
  73. if (isset($_GET['accesscheck'])) {
  74. $_SESSION['PrevUrl'] = $_GET['accesscheck'];
  75. }
  76.  
  77. if (isset($_POST['login'])) {
  78. $loginUsername=$_POST['login'];
  79. $password=$_POST['password'];
  80. $MM_fldUserAuthorization = "active";
  81. $MM_redirectLoginSuccess = "profile.php";
  82. $MM_redirectLoginFailed = "login.php";
  83. $MM_redirecttoReferrer = false;
  84. mysql_select_db($database_myconn, $myconn);
  85.  
  86. $LoginRS__query=sprintf("SELECT username, password, active FROM userlogin WHERE username=%s AND password=%s",
  87. GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text"));
  88.  
  89. $LoginRS = mysql_query($LoginRS__query, $myconn) or die(mysql_error());
  90. $loginFoundUser = mysql_num_rows($LoginRS);
  91. if ($loginFoundUser) {
  92.  
  93. $loginStrGroup = mysql_result($LoginRS,0,'active');
  94.  
  95. if (PHP_VERSION >= 5.1) {session_regenerate_id(true);} else {session_regenerate_id();}
  96. //declare two session variables and assign them
  97. $_SESSION['MM_Username'] = $loginUsername;
  98. $_SESSION['MM_UserGroup'] = $loginStrGroup;
  99.  
  100. if (isset($_SESSION['PrevUrl']) && false) {
  101. $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];
  102. }
  103. header("Location: " . $MM_redirectLoginSuccess );
  104. }
  105. else {
  106. header("Location: ". $MM_redirectLoginFailed );
  107. }
  108. }
  109. ?>
  110. <form action="<?php echo $loginFormAction; ?>" method="POST">
  111. <div id="formularz">
  112. <div align="center">
  113. <p>&nbsp;</p>
  114. <p>&nbsp;</p>
  115. <p>&nbsp;</p>
  116. <p>&nbsp;</p>
  117. <p>&nbsp;</p>
  118. <p>&nbsp;</p>
  119. <p>Login:
  120. <input type="text" name="login" /><br /> Hasło:
  121. <input type="password" name="password" /><br />
  122. <input type="submit" value="Zaloguj" />
  123. </p>
  124. </div>
  125. </div>
  126. </form>
  127.  

  1. CREATE TABLE IF NOT EXISTS `userlogin` (
  2. `user_id` int(11) NOT NULL AUTO_INCREMENT,
  3. `firstname` varchar(32) NOT NULL,
  4. `lastname` varchar(32) NOT NULL,
  5. `username` varchar(32) NOT NULL,
  6. `password` varchar(32) NOT NULL,
  7. `email` varchar(256) NOT NULL,
  8. `dateregister` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  9. `lastlogin` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  10. `active` int(11) NOT NULL DEFAULT '0',
  11. `COLOR` varchar(6) DEFAULT NULL,
  12. `ip` varchar(15) NOT NULL,
  13. PRIMARY KEY (`user_id`),
  14. UNIQUE KEY `username` (`username`,`email`)
  15. ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=217 ;
piotrex41
Proponował bym dodać zapytanie sql, a konkretnie UPDATE z aktualizacją daty ostatniego logowania. Zapytanie to wstawiłbym w momencie zalogowania danego użytkownika (tworzenia się sesji). Dzięki temu przy każdym logowaniu każdego użytkownika będzie wprowadzana do bazy aktualizacja daty ostatniego logowania.

Myślę że zapytanie dasz radę napisać sam smile.gif
badziol1
Właśnie nie za bardzo wiem jak zadać to pytanie dotyczące tej bazy. może coś takiego
  1. $query = mysql_query("UPDATE userlogin SET lastlogin=NOW()
ale wtedy wypełnia mi całą kolumnę lastlogin.
thek
A o WHERE user = 'xxxx' nie zapomniałeś przypadkiem wink.gif
badziol1
Zaraz spróbuję

Dałem i nic z tego
  1. $query = mysql_query("UPDATE userlogin SET lastlogin=NOW() WHERE user_id='xxxx' ");

na różne sposoby były i 3 xxx

TAk przeglądam ten artykuł i nikt nie potrafi pomóc. Gdzie tkwi błąd.
com
ty chyba nie mówisz poważnie za xxx masz wstawić zmienna przechowującą id użytkownika np

  1. $id = //jakoś pobrany ten nr np z bazy danych na podstawie zalogowanego użytkownika
  2. $query = mysql_query("UPDATE userlogin SET lastlogin=NOW() WHERE user_id='$id' ");
badziol1
$id = //jakoś pobrany ten nr np z bazy danych na podstawie zalogowanego użytkownika co w tym miejscu wpisać $id=?
sesję wpisać np.$_SESSION[MM_Username']
com
Nie bo user_id odwołuje się do nr użytkownika a nie jego nazwy wiec jak tak chcesz to musiałbyś dać:

  1. $user = $_SESSION['MM_Username'];
  2. $query = mysql_query("UPDATE userlogin SET lastlogin=NOW() WHERE username='$user' ");
badziol1
Jest prawie okey zapisauje do bazy ale jeszcze wyskakuje taki komunikat
Notice: Undefined index: MM_Username in C:\xampp\htdocs\woxa\_admin\login.php on line 120
com
To wystarczy zrobić tak, bo php porostu informuje cie o tym, że w przypadku gdy jeszcze nie stworzyłeś sesji, odnosisz się do czegoś co nie istnieje...
  1. if (isset($_SESSION['MM_Username'])) {
  2. $user = $_SESSION['MM_Username'];
  3. $query = mysql_query("UPDATE userlogin SET lastlogin=NOW() WHERE username='$user' ");
  4. }
badziol1
super działa. Dziękuje i pozdrawiam
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.