Hi,

I'm fairly new to php and am having a lot of trouble with a simple little select problem. I have tried it about 20 different ways and it just keeps spitting out errors and not working

My code is:

<?php 
session_start(); // built in php function to start the session array

	require_once('../includes/admin/db.php'); // database connection
	$conn = dbConnect('admin');

	if(isset($_POST['login'])) {
		$sql='SELECT * FROM admin WHERE username=? AND passwd=?';
		$pds=$conn->prepare($sql);
		$pds->execute(array($_POST['username'],(sha1($_POST['passwd'])))); 
		if($pds->fetchColumn(0)) { 	// Success
			$_SESSION['admin']=$_POST['username'];
			
		}
		else { 				// Failure
			$error='<p>Unsuccessful Login</p>';
			
		}
	}
	elseif(isset($_POST['logout'])) {
		unset($_SESSION['admin']); // closes the admin session
		setcookie(session_name(),'',time()-86400); 
		session_destroy(); 
	}
--------
Line 27 $username = $_POST['username'];
$sql = 'SELECT team FROM users WHERE username = "$username"';
if (!$sql) {
    echo 'Could not run query: ' . mysql_error();
    exit;
}
$teamName = mysql_fetch_row($sql);

All I want to do is make $teamName the name of the team that that user is editing. just can't figure out what I am doing wrong. All the code above the -------- works fine, but the stuff below is the problem.
the main error that I am having is: Notice: Undefined index: username in C:\Users\...\dash.php on line 27
It should be easy to do I thought, but I have spent about 12 hours trying different things with no luck.

Please help, if you could give me the code I should be using that would be awesome.

Thanks,

QWaz

Recommended Answers

All 6 Replies

I seems like you might have your error reporting set to show E_NOTICE level errors. When error reporting is set to this level it will pester you about trying to use unassigned values amongst other things. If this is the case changing your error reporting level would correct this.

// Turn off all error reporting
error_reporting(0);

Also, your query looks like it's set up to fail.

$sql = mysql_query("SELECT team FROM users WHERE username = '$username' ");
Member Avatar for soldierflup

The reason why it's not working is because you don't run the query.
With the variable $sql you just define the query, you don't run it.

Try this :

$sql = "SELECT team FROM users WHERE username = '$username'";
$result = mysql_query($result) or die(mysql_error());
if (!$result) {
echo "No result";
exit;
} else {
while ($row = mysql_fetch_assoc($result)) {
$teamName = $row['team'];
}
}

Just a quick correction or the query will still fail.

$sql = "SELECT team FROM users WHERE username = '$username'";
$result = mysql_query($result) or die(mysql_error()); ///// <----- mysql_query($sql)
if (!$result) {
echo "No result";
exit;
} else {
while ($row = mysql_fetch_assoc($result)) {
$teamName = $row['team'];
}
}
Member Avatar for soldierflup

Thanks for correcting.
I was a little to fast

I figured as much, just lookin' out ;)

Hey Guys,

Sorry but this code doesn't work either.

I am getting these messages:
Notice: Undefined variable: username in C:\...\dash.php on line 28

Warning: mysql_query() [function.mysql-query]: Access denied for user 'SYSTEM'@'localhost' (using password: NO) in C:\...\dash.php on line 29

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established inC:\...\dash.php on line 29
Access denied for user 'SYSTEM'@'localhost' (using password: NO)

Does this code work for MySQL PDO? or is this code for the older versions of MySQL?

The other constant issue I am having is, the username being a undefined variable. So in that case how do I get the username into the $username variable?
it works in the first log in part, so why is is not working here?

Thanks for you help,

Cheers,

Qwaz

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.