Page 1 of 1

Trouble inserting values in to table after login...

Posted: Sun Sep 25, 2011 7:58 pm
by orbdrums
The following code displays validating login and password. If true then "member-index.php" is called. I want to insert values in a table called "last-login" but, although the code seems to function properly, nothing gets populated in the "last-logn" table. As always, I appreciate your help.

Code: Select all

	//Create query
	$qry="SELECT * FROM members WHERE login='$login' AND passwd='".md5($_POST['password'])."'";
	$result=mysql_query($qry);
	
	//Check whether the query was successful or not
	if($result) {
		if(mysql_num_rows($result) == 1) {
			//Login Successful
                     [color=#BF0000]$query="INSERT INTO `Users`.`last_login` (`member_id`, `login_time`, `login_date`, `logout_time`, `logout_date`) VALUES (2, CURTIME(), CURDATE(), \'\', \'\')";
                     mysql_query($query);[/color]
			session_regenerate_id();
			$member = mysql_fetch_assoc($result);
			$_SESSION['SESS_MEMBER_ID'] = $member['member_id'];
			$_SESSION['SESS_FIRST_NAME'] = $member['firstname'];
			$_SESSION['SESS_LAST_NAME'] = $member['lastname'];
                     
			session_write_close();
			header("location: member-index.php");
			exit();
		}else {
			//Login failed
			header("location: login-failed.php");
			exit();
		}
	}else {
		die("Query failed");
	}
Clark

Re: Trouble inserting values in to table after login...

Posted: Sun Sep 25, 2011 9:02 pm
by danwguy
I am surprised you aren't getting errors with that... not sure why you aren't telling php that the <span> is an echo... should be this...

Code: Select all

//Create query
        $qry="SELECT * FROM members WHERE login='$login' AND passwd='".md5($_POST['password'])."'";
        $result=mysql_query($qry);
        
        //Check whether the query was successful or not
        if($result) {
                if(mysql_num_rows($result) == 1) {
                        //Login Successful
                  echo "<span style="color: #BF0000">";
                           $query="INSERT INTO `Users`.`last_login` (`member_id`, `login_time`, `login_date`, `logout_time`, `logout_date`) VALUES (2, CURTIME(), CURDATE(), \'\', \'\')";
                           @mysql_query($query);
                 echo "</span>";
                        session_regenerate_id();
                        $member = mysql_fetch_assoc($result);
                        $_SESSION['SESS_MEMBER_ID'] = $member['member_id'];
                        $_SESSION['SESS_FIRST_NAME'] = $member['firstname'];
                        $_SESSION['SESS_LAST_NAME'] = $member['lastname'];
                     
                        session_write_close();
                        header("location: member-index.php");
                        exit();
                }else {
                        //Login failed
                        header("location: login-failed.php");
                        exit();
                }
        }else {
                die("Query failed");
        }
that's assuming you already made your sql connection before all of this. try adding those echo statements and using the @ infront of your mysql_query statement. You could also assign it to a variable eg $run = mysql_query($query); then say if($run) { } else { echo mysql_error(); } that way you can see what error is being given

Re: Trouble inserting values in to table after login...

Posted: Sun Sep 25, 2011 10:45 pm
by orbdrums
Sorry about the <span> tag. That was generated by this forum. Everything worked out with this code:

Code: Select all

	//Create query
	$qry="SELECT * FROM members WHERE login='$login' AND passwd='".md5($_POST['password'])."'";
	$result=mysql_query($qry);
	
	//Check whether the query was successful or not
	if($result) {
		if(mysql_num_rows($result) == 1) {
			//Login Successful
                     session_regenerate_id();

                     $query = "INSERT INTO Users.last_login(member_id, login_time, login_date) VALUES('2',CURTIME(),CURDATE())";
	              @mysql_query($query);


			$member = mysql_fetch_assoc($result);
			$_SESSION['SESS_MEMBER_ID'] = $member['member_id'];
			$_SESSION['SESS_FIRST_NAME'] = $member['firstname'];
			$_SESSION['SESS_LAST_NAME'] = $member['lastname'];
                     
			session_write_close();
			header("location: member-index.php");
			exit();
		}else {
			//Login failed
			header("location: login-failed.php");
			exit();
		}
	}else {
		die("Query failed");
	}
Clark

Re: Trouble inserting values in to table after login...

Posted: Sun Sep 25, 2011 10:59 pm
by orbdrums
This worked....

Code: Select all

	//Create query
	$qry="SELECT * FROM members WHERE login='$login' AND passwd='".md5($_POST['password'])."'";
	$result=mysql_query($qry);
	
	//Check whether the query was successful or not
	if($result) {
		if(mysql_num_rows($result) == 1) {
			//Login Successful
                     session_regenerate_id();

                     $query = "INSERT INTO Users.last_login(member_id, login_time, login_date) VALUES('2',CURTIME(),CURDATE())";
	              @mysql_query($query);


			$member = mysql_fetch_assoc($result);
			$_SESSION['SESS_MEMBER_ID'] = $member['member_id'];
			$_SESSION['SESS_FIRST_NAME'] = $member['firstname'];
			$_SESSION['SESS_LAST_NAME'] = $member['lastname'];
                     
			session_write_close();
			header("location: member-index.php");
			exit();
		}else {
			//Login failed
			header("location: login-failed.php");
			exit();
		}
	}else {
		die("Query failed");
	}