Jump to content

Insert into database on login


supermerc

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.