Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP Databases
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
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.



 
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




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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.