supermerc Posted March 24, 2008 Share Posted March 24, 2008 Hey Im trying to enter the time when the user logs in into the database but its not working. this is my code <?php ob_start(); session_start(); include 'config.php'; if(isset($_POST['login'])) { $email = trim(addslashes($_POST['email'])); $password = md5(trim($_POST['password'])); $query = mysql_query("SELECT * FROM users WHERE email = '$email' AND password = '$password' LIMIT 1") or die(mysql_error()); $row = mysql_fetch_array($query); // now we check if they are activated if(mysql_num_rows($query) > 0) { if($row['activated'] > 0) { $q = "UPDATE `users` SET lastlogin = NOW() WHERE id = '".$row['id']."'"; $do = mysql_query($q) or die(mysql_error()."<br /><br />".$q); $_SESSION['time'] = $row['lastlogin']; $_SESSION['s_logged_n'] = 'true'; $_SESSION['s_email'] = $email; $_SESSION['s_id'] = $row['id']; $_SESSION['s_name'] = $row['name']; $_SESSION['s_level'] = $row['level']; // Store user level in SESSION header("Location: member.php"); exit(); It gives me no errors but doesnt update lastlogin from the database. Link to comment https://forums.phpfreaks.com/topic/97659-insert-into-database-on-login/ Share on other sites More sharing options...
rhodesa Posted March 24, 2008 Share Posted March 24, 2008 What is the field type for lastlogin? Link to comment https://forums.phpfreaks.com/topic/97659-insert-into-database-on-login/#findComment-499686 Share on other sites More sharing options...
supermerc Posted March 24, 2008 Author Share Posted March 24, 2008 datetime Link to comment https://forums.phpfreaks.com/topic/97659-insert-into-database-on-login/#findComment-499687 Share on other sites More sharing options...
rhodesa Posted March 24, 2008 Share Posted March 24, 2008 And, after this line: $q = "UPDATE `users` SET lastlogin = NOW() WHERE id = '".$row['id']."'"; if you put: print $q;exit; What does the generated query look like... Link to comment https://forums.phpfreaks.com/topic/97659-insert-into-database-on-login/#findComment-499690 Share on other sites More sharing options...
supermerc Posted March 24, 2008 Author Share Posted March 24, 2008 well it doesnt do anything because the login was sucessful so it redirects me to members.php because of header("Location: member.php"); exit(); Link to comment https://forums.phpfreaks.com/topic/97659-insert-into-database-on-login/#findComment-499694 Share on other sites More sharing options...
rhodesa Posted March 24, 2008 Share Posted March 24, 2008 i had a print and an exit in there. did you get them both in there? the script should halt after printing the query Link to comment https://forums.phpfreaks.com/topic/97659-insert-into-database-on-login/#findComment-499701 Share on other sites More sharing options...
supermerc Posted March 24, 2008 Author Share Posted March 24, 2008 ya i put it, i put exactly what you told me and it still brings me to members.php Link to comment https://forums.phpfreaks.com/topic/97659-insert-into-database-on-login/#findComment-499704 Share on other sites More sharing options...
rhodesa Posted March 24, 2008 Share Posted March 24, 2008 Impossible. Are you sure a value is getting passed via POST? Can you post the rest of the code below that to finish off the IF statements? Link to comment https://forums.phpfreaks.com/topic/97659-insert-into-database-on-login/#findComment-499712 Share on other sites More sharing options...
supermerc Posted March 24, 2008 Author Share Posted March 24, 2008 Sure, there u go <?php ob_start(); session_start(); include 'config.php'; if(isset($_POST['login'])) { $email = trim(addslashes($_POST['email'])); $password = md5(trim($_POST['password'])); $query = mysql_query("SELECT * FROM users WHERE email = '$email' AND password = '$password' LIMIT 1") or die(mysql_error()); $row = mysql_fetch_array($query); // now we check if they are activated if(mysql_num_rows($query) > 0) { if($row['activated'] > 0) { $q = "UPDATE `users` SET lastlogin = NOW() WHERE id = '".$row['id']."'"; print $q;exit; $do = mysql_query($q) or die(mysql_error()."<br /><br />".$q); $_SESSION['time'] = $row['lastlogin']; $_SESSION['s_logged_n'] = 'true'; $_SESSION['s_email'] = $email; $_SESSION['s_id'] = $row['id']; $_SESSION['s_name'] = $row['name']; $_SESSION['s_level'] = $row['level']; // Store user level in SESSION header("Location: member.php"); exit(); } else { echo ' <html> <head> <title>Login</title> <link href="style.css" rel="stylesheet" type="text/css"> </head> <body> <div id="error"><p>Sorry, you must activate your account first. Please check your email for the email.</p> <p>Didn'."'".'t get your validation email? <a href="resend.php">Click here</a> to resend the validation email.</p></div> </body> </html> '; } } else { echo ' <html> <head> <title>Login</title> <link href="style.css" rel="stylesheet" type="text/css"> </head> <body> <div id="error"><p>There was an error processing your login, it appears that your username and/or password was incorrect. Please try again.</p> <p>Didn'."'".'t get your validation email? <a href="resend.php">Click here</a> to resend the validation email.</p> </div> </body> </html> '; } } else { ob_end_flush(); ?> <html> <head> <title>Login</title> <link href="style.css" rel="stylesheet" type="text/css"> </head> <body> <div id="wrapper"> <div id="head">the login page</div><br> <div id="main"> <p>You must login to view this page. Enter your username and password below and hit submit:</p> <form method="post" action="<?= $_SERVER['PHP_SELF'] ?>"> <p>Username:<br> <input name="username" type="text" class="textBox" id="username"> <p>Password:<br> <input name="password" type="password" class="textBox" id="password"> </p> <p> <input name="login" type="submit" class="textBox" id="login" value="Submit"> </p> </form> <p>Didn't get your validation email? <a href="resend.php">Click here</a> to resend the validation email.</p> <p>Need an account? <a href="register.php">Click here</a> to register, it's completely free! </p> </div> </div> </body> </html> <? } mysql_close($l); ?> Link to comment https://forums.phpfreaks.com/topic/97659-insert-into-database-on-login/#findComment-499734 Share on other sites More sharing options...
rhodesa Posted March 24, 2008 Share Posted March 24, 2008 Ah, why are you using ob_* functions? Try this: <?php session_start(); include 'config.php'; if(isset($_POST['login'])) { $email = trim(addslashes($_POST['email'])); $password = md5(trim($_POST['password'])); $query = mysql_query("SELECT * FROM users WHERE email = '$email' AND password = '$password' LIMIT 1") or die(mysql_error()); $row = mysql_fetch_array($query); // now we check if they are activated if(mysql_num_rows($query) > 0) { if($row['activated'] > 0) { $q = "UPDATE `users` SET lastlogin = NOW() WHERE id = '".$row['id']."'"; $do = mysql_query($q) or die(mysql_error()."<br /><br />".$q); $_SESSION['time'] = $row['lastlogin']; $_SESSION['s_logged_n'] = 'true'; $_SESSION['s_email'] = $email; $_SESSION['s_id'] = $row['id']; $_SESSION['s_name'] = $row['name']; $_SESSION['s_level'] = $row['level']; // Store user level in SESSION header("Location: member.php"); exit; } else { echo ' <html> <head> <title>Login</title> <link href="style.css" rel="stylesheet" type="text/css"> </head> <body> <div id="error"><p>Sorry, you must activate your account first. Please check your email for the email.</p> <p>Didn'."'".'t get your validation email? <a href="resend.php">Click here</a> to resend the validation email.</p></div> </body> </html> '; } } else { echo ' <html> <head> <title>Login</title> <link href="style.css" rel="stylesheet" type="text/css"> </head> <body> <div id="error"><p>There was an error processing your login, it appears that your username and/or password was incorrect. Please try again.</p> <p>Didn'."'".'t get your validation email? <a href="resend.php">Click here</a> to resend the validation email.</p> </div> </body> </html> '; } } else { ?> <html> <head> <title>Login</title> <link href="style.css" rel="stylesheet" type="text/css"> </head> <body> <div id="wrapper"> <div id="head">the login page</div><br> <div id="main"> <p>You must login to view this page. Enter your username and password below and hit submit:</p> <form method="post" action="<?= $_SERVER['PHP_SELF'] ?>"> <p>Username:<br> <input name="username" type="text" class="textBox" id="username"> <p>Password:<br> <input name="password" type="password" class="textBox" id="password"> </p> <p> <input name="login" type="submit" class="textBox" id="login" value="Submit"> </p> </form> <p>Didn't get your validation email? <a href="resend.php">Click here</a> to resend the validation email.</p> <p>Need an account? <a href="register.php">Click here</a> to register, it's completely free! </p> </div> </div> </body> </html> <? } mysql_close($l); ?> Link to comment https://forums.phpfreaks.com/topic/97659-insert-into-database-on-login/#findComment-499749 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.