Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > PHP Databases
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
PHP Databases Using PHP in conjunction with databases. PHP questions not specific to databases should be directed to one of the other PHP forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP Databases section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 14th, 2008, 12:52 AM
Registered User
 
Join Date: Apr 2008
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Notice: Undefined index: username in

Hi all,

im not sure if this is the correct place to post so if it is not, please direct it to where it supposed to be.

Im getting this error:

Notice: Undefined index: username in /clientdata/clients/x/b/xbs.com.au/www/stafflogin/login.php on line 6

and this is my coding:

<?php
include('db.php');
include('sess.php');
session_start();
include('db.php');
if ($_POST['username'] && $_POST['password']) { //<-- line 6
  $username = mysql_real_escape_string($_POST['username']);
  $password = md5(mysql_real_escape_string($_POST['password']));
  $query = "SELECT * FROM users WHERE username='" . $username . "' AND password='" . $password . "'";
  $result = mysql_query($query);
  if (!$result) {
    echo "Could not successfully run query ($query) from DB: " . mysql_error();
    exit;
  }
  $site = dirname($_SERVER['SCRIPT_NAME']);
  $site = str_replace('/','',$site);
  $user = mysql_fetch_assoc($result);

  if ($user && $user['site_name']==$site){
    $_SESSION['user']['id'] = $user['id'];
    $_SESSION['user']['username'] = $user['username'];
    $_SESSION['user']['group_id'] = $user['group_id'];

it continues but the error is on line 6.

IF anyone could help me out with this problem it would be greatly appreciated..

Cheers.



Reply With Quote
  #2 (permalink)  
Old April 18th, 2008, 11:37 AM
Authorized User
 
Join Date: Sep 2007
Location: , , .
Posts: 56
Thanks: 0
Thanked 1 Time in 1 Post
Default

Its because the first time you load the page nothing has been posted so it doesn't know what username is. Also that isn't really an error that would stop the program...just a notice message (this means you have your error reporting level set to show all types of messages)...warnings,notices,errors...etc

Change your code to this:
Code:
<?php
include('db.php');
include('sess.php');
session_start();
include('db.php');
$username = isset($_POST['username']) ? $_POST['username'] : '';
$password = isset($_POST['password']) ? $_POST['password'] : '';
if ($username != '' && $password != '') {   //<-- line 6
  $username = mysql_real_escape_string($username);
  $password = md5(mysql_real_escape_string($password));
  $query = "SELECT * FROM users WHERE username='" . $username . "' AND password='" . $password . "'";
  $result = mysql_query($query);
  if (!$result) {
    echo "Could not successfully run query ($query) from DB: " . mysql_error();
    exit;
  }
  $site = dirname($_SERVER['SCRIPT_NAME']);
  $site = str_replace('/','',$site);
  $user = mysql_fetch_assoc($result);

  if ($user && $user['site_name']==$site){
    $_SESSION['user']['id'] = $user['id'];
    $_SESSION['user']['username'] = $user['username'];
    $_SESSION['user']['group_id'] = $user['group_id'];

http://arizonawebdevelopment.com
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Notice: Undefined variable error on input form tonyb187 Pro PHP 2 May 11th, 2008 06:58 AM
Notice: Undefined variable: comments/ Form reset dungey Pro PHP 10 December 1st, 2006 04:00 AM
Notice: Undefined offset: ## Herjan BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 0 November 27th, 2005 12:29 PM
Loops and Arrays - Undefined offset notice dkintheuk Beginning PHP 6 September 12th, 2005 11:38 PM
Undefined index ashinotani Beginning PHP 2 August 17th, 2004 03:31 PM



All times are GMT -4. The time now is 07:56 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.