Jump to content

[SOLVED] loading mysql data into a variable


wargolchaos

Recommended Posts

Goal

assign user level into a session when a user logs in.

 

login mysql code:

// Verify
		$email = mysql_escape_string($_POST['email']);
		$password = md5($_POST['password']);

		$gUser = mysql_query("SELECT * FROM users WHERE email='".$email."' AND password='".$password."' LIMIT 1") or die(mysql_error());
		$verify = mysql_num_rows($gUser);

		if($verify > 0){
			echo '<h2>Login Complete</h2>';

		}else{
			echo '<h2>Login Failed</h2>
			      <p>Sorry your login credentials are incorrect.<br /><br />';
		}

 

So what I need to do is assign a level variable,

This is what I'm thinking.

$level = (a db query) [-the level will be between 1 and 5-]

then assign it to a session var

$_SESSION["level"] = $level

 

So I am looking to learn a reliable way to set this variable and have it stick as a session.

Its not working.

 

Heres what the entire script looks like.

<?php
if(isset($_SESSION["level"])) {			
            echo '<h3>Admin</h3>
            <p>This is an example of a content page!</p>';
		echo '<h3>Welcome '.$_SESSION["name"].', your user level is '.$_SESSION["level"];
} else {
	  include('db_info.php');
	connectDB();
	selectDB("-----");

	if(isset($_POST['email']) && isset($_POST['password'])){
		// Verify
		$email = mysql_escape_string($_POST['email']);
		$password = md5($_POST['password']);

		$gUser = mysql_query("SELECT * FROM users WHERE email='".$email."' AND password='".$password."' LIMIT 1") or die(mysql_error());
		$verify = mysql_num_rows($gUser);

		if($verify > 0){
			/*Set user level*/
			$row = mysql_fetch_assoc($gUser);
			$_SESSION["level"] = $_row["level"];
			$_SESSION["name"] = $email;
			/*End of set user level*/
			echo '<h2>Login Complete</h2>';
			echo '<h3>Welcome '.$_SESSION["name"].', your user level is '.$_SESSION["level"];
		}else{ /*	If login fails> */
			echo '<h2>Login Failed</h2>
			      <p>Sorry your login credentials are incorrect. <a href=\"admin.php\">Try again</a><br /><br />';
		}
	}else{
	?>

 

heres what my db looks like

$sql ="CREATE TABLE users (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  username VARCHAR(50) NOT NULL,
  email VARCHAR(50) NOT NULL,
  password VARCHAR(32) NOT NULL,
  date DATE NOT NULL,
  level INT NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";

 

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.