Undefined index error and login always "true"

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jmcc
Forum Newbie
Posts: 5
Joined: Wed May 20, 2009 5:47 am

Undefined index error and login always "true"

Post by jmcc »

Hi,

Below you will find the code I wrote for a login script. I am new at php please bear this in mind.

Errors received:
Notice: Undefined index: userid in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\login.php on line 5

Notice: Undefined index: password in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\login.php on line 6

Notice: Undefined index: submitted in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\login.php on line 7

Notice: Use of undefined constant rowAccount - assumed 'rowAccount' in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\login.php on line 17
The result is also true before any username and password is entered.

Code for login.php:

Code: Select all

 
<?php
require_once("connection.php"); // database connection
 
// catch field data
$userid         =   $_POST['userid'];
$password       =   $_POST['password'];
$submitted      =   $_POST['submitted'];
 
if ($userid && $password) {
//////////////////////////////////
$query     =sprintf("SELECT * FROM where user_name='$userid' and user_password = '$password'");
$result    =@mysql_query($query);
$rowAccount =@mysql_fetch_array($result);
/////////////////////////////////
}
 
if (rowAccount) {
 
echo "The record exists so you can enter";
 
}elseif($submitted){
 
echo "You dont exists on our record";
 
}
?>
 
Code for connections.php

Code: Select all

 
<?php
///////////////////////////
$database   =  "test";
$username   =  "username";
$password   =  "password";
//////////////////////////
 
$link   =@mysql_connect('localhost', $username, $password);
$db     =mysql_select_db($database, $link);
 
?>
 
I would appreciate any help.
Jay
Last edited by Benjamin on Wed May 20, 2009 8:03 am, edited 1 time in total.
Reason: Changed code type from text to php.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Undefined index error and login always "true"

Post by Mark Baker »

change

Code: Select all

$userid = $_POST['userid'];
to

Code: Select all

$userid = (isset($_POST['userid'])) ? $_POST['userid'] : false;
for each such occurrence
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Undefined index error and login always "true"

Post by Darhazer »

I already replied about rowAccount here:
viewtopic.php?f=1&t=100501
Post Reply