Jump to content

Another Login System Question?


JeffC

Recommended Posts

Hi,I have finished making the multiple user account interface, but, now I am having trouble with another feature of the interface.The purpose of the code that I have added, is to reference the "shared" table of the database. But when I try to call a cell from the table farther below, I get the following error:Notice: Undefined index: shared in [filepath] on line 134. What I am currently trying to do to call the second table and then store it as a variable, is this:

$query = "  SELECT * FROM shared  ";$_data = ['shared'];

The error on line 134 is caused by the following code:

<?php echo htmlentities($_data['shared']['variable'], ENT_QUOTES, 'UTF-8'); ?>

The full relevant code is below.

<!doctype html><?php	require("common.php");	$submitted_username = '';$query = "  SELECT * FROM shared  ";$_data = ['shared'];	if(!empty($_POST))	{		$query = "			SELECT				id,				username,				password,				salt,				email,	firstname,	lastname,	accountType			FROM users			WHERE				username = :username		";		$query_params = array(			':username' => $_POST['username']		);				try		{			$stmt = $db->prepare($query);			$result = $stmt->execute($query_params);		}		catch(PDOException $ex)		{ .			die("Failed to run query: " . $ex->getMessage());		}				$login_ok = false;				$row = $stmt->fetch();		if($row)		{			$check_password = hash('sha256', $_POST['password'] . $row['salt']);			for($round = 0; $round < 65536; $round++)			{				$check_password = hash('sha256', $check_password . $row['salt']);			}						if($check_password === $row['password'])			{				$login_ok = true;			}		}				if($login_ok)		{			unset($row['salt']);			unset($row['password']);			$_SESSION['user'] = $row;						header("Location: private.php");			die("Redirecting to: private.php");		}		else		{			print("Login Failed.");  			$submitted_username = htmlentities($_POST['username'], ENT_QUOTES, 'UTF-8');		}	}  ?><body data-accent="blue">  <!-- Prompt IE 6 users to install Chrome Frame. Remove this if you support IE 6.	   chromium.org/developers/how-tos/chrome-frame-getting-started -->  <!--[if lt IE 7]><p class=chromeframe>Your browser is <em>ancient!</em> <a href="http://browsehappy.com/">Upgrade to a different browser</a> or <a href="http://www.google.com/chromeframe/?redirect=true">install Google Chrome Frame</a> to experience this site.</p><![endif]-->   <header id="nav-bar" class="container-fluid">	  <div class="row-fluid">		 <div class="span8">			<div id="header-container">			   <div class="dropdown">				  <a class="header-dropdown dropdown-toggle accent-color" data-toggle="dropdown" href="#">					 Cloud<?php echo htmlentities($_data['shared']['variable'], ENT_QUOTES, 'UTF-8'); ?>				  </a>			</div>			</div>		 </div>   </div>   </header>   <div class="container-fluid">	  <div class="row-fluid">		 <div style="text-align: center;" class="metro span12">	<p>Woah there champ! Before you move any further, you need to be logged in!</p>	<form action="login.php" method="post">  Username:<br />  <input type="text" name="username" value="<?php echo $submitted_username; ?>" />  <br /><br />  Password:<br />  <input type="password" name="password" value="" />  <br /><br />	   <input type="submit" class="btn btn-primary" value="Log Me In!">	</form>

If someone could help that would be great!

Link to comment
Share on other sites

After you do this:

$query = "SELECT * FROM shared";$_data = ['shared'];

Assuming you're using a recent version of PHP, then $_data will be an array with the string "shared" in the first index: $_data = array(0 => 'shared'); That's why $_data['shared'] is undefined. If you want to make a sub-array with the key 'shared', then it looks like this: $_data = array('shared' => array()); That being said, if you expect $_data to hold data from the database, then you also need to execute the query and populate $_data with the result. You define that variable called $query but then you don't do anything with it, including actually running it.

Link to comment
Share on other sites

After you do this:
$query = "SELECT * FROM shared";$_data = ['shared'];

Assuming you're using a recent version of PHP, then $_data will be an array with the string "shared" in the first index: $_data = array(0 => 'shared'); That's why $_data['shared'] is undefined. If you want to make a sub-array with the key 'shared', then it looks like this: $_data = array('shared' => array()); That being said, if you expect $_data to hold data from the database, then you also need to execute the query and populate $_data with the result. You define that variable called $query but then you don't do anything with it, including actually running it.

So what I should do, is switch $query, with $_data, and then, after$_data = "SELECT * FROM shared";$_data = ['shared'];, I need to add $_data = array('shared' => array()); below this?
Link to comment
Share on other sites

It doesn't matter what you name the variable that holds the query, but if you want to get data from the database then you need to actually run the query. Defining a variable that contains a query isn't enough to interact with a database, you need to actually connect to the database and send the query to it. You're executing another query on the page to get the user information, that's how to execute a query. Just defining a variable that contains a query doesn't execute the query on a database. After you run the query then you can get the data and build an array to contain whatever data you want from the database.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...