How to fix "Notice : undefined index"

Started on the old forum in September 2011 · view the original in the archive · old links redirect here

#database #debug #debugging #error #php

5 replies · 2,628 views · started 15 years ago · 1 min read

3 participants
s3nzo Developer · 156 posts #1

Hey guys i'm getting an error using this code to update user details in a database

<?php
	require	'inc/head.inc';
	if(!isset($_SESSION['id']))
		header('Location:index.php');
	else
		{
			require 'inc/header.inc';
				
				print	'Update profile or leave blank if you don\'t want to update';
				
				$sql	=	"SELECT * FROM users WHERE id	=	'{$_SESSION['id']}'";
				$query	=	@mysql_query($sql, $con);
				$result	=	@mysql_fetch_array($query);
						
						if(isset($_SESSION['errors']))
								if(is_array($_SESSION['errors']))
									foreach($_SESSION['errors'] as $error)
										print	$error;
										
							if(!isset($_POST['count']))
							{
								print	'<form action="" method=post>
										<div class=dl>First Name</div>
										<input type=text name=fname size=10 maxlength=30>
										<div class=dl>Nick</div>
										<input type=text name=nick size=10 maxlength=20>
										<div class=dl>Last Name</div>
										<input type=text name=sname size=10 maxlength=30>
										<input type=hidden name=count>
										<div class=dl>Gender</div>
										<select name=sex>
											<option value=m>Male</option>
											<option value=f>Female</option>
										</select><br>
										<div class=dl>Country</div>
										<input type=text name=country size=10 maxlength=30>
										<div class=dl>City</div>
										<input type=text name=city size=10 maxlength=30>
										<div class=dl>Relationship</div>
										<select name=relationship>
											<option value="single">Single</option>
											<option value="in a relationship">In a relationship</option>
											<option value="engaged">Engaged</option>
											<option value="married">Married</option>
											<option value="available">Available</option>
										</select><br>
										<input class=button type=submit value=Update>
										</form>';
										
							}
							
							else
								{
									$_SESSION['fname']			=	@mysql_real_escape_string(htmlentities(stripslashes($_POST['fname'])));
									$_SESSION['sname']			=	@mysql_real_escape_string(htmlentities(stripslashes($_POST['sname'])));
									$_SESSION['nick']			=	@mysql_real_escape_string(htmlentities(stripslashes($_POST['nick'])));
									$_SESSSION['sex']			=	@mysql_real_escape_string(htmlentities(stripslashes($_POST['sex'])));
									$_SESSSION['country']		=	@mysql_real_escape_string(htmlentities(stripslashes($_POST['country'])));
									$_SESSSION['city']			=	@mysql_real_escape_string(htmlentities(stripslashes($_POST['city'])));
									$_SESSSION['relationship']	=	@mysql_real_escape_string(htmlentities(stripslashes($_POST['relationship'])));
										
										$error	=	array();
										if(strlen($_SESSION['year']) != 4)
											$error	=	'<br>Year must be a 4 digit';
										if(strlen($_SESSION['month']) != 2 || $_SESSION['month'] < 1 || $_SESSION['month'] > 12)
											$error	=	'<br>Enter a valid month';
										if(strlen($_SESSION['day']) != 2 || $_SESSION['day'] < 1 || $_SESSION['day'] > 31)
											$error	=	'<br>You entered an invalid day';
										if(count($error))
											{
														$_SESSION['error'] 	=	$error;
														if(is_array($_SESSION['error']))
															{
																foreach($_SESSION['error'] as $errors)
																	print	$errors;
																	
															}
														else
															print $_SESSION['error'];
															unset($_SESSION['error']);
															unset($_POST['count']);
											}
										else
											{
#Seems like my problem is below this comment
												$update	=	"UPDATE users SET	`fname`			=	'{$_SESSION['fname']}',
																				`sname`			=	'{$_SESSION['sname']}',
																				`nick`			=	'{$_SESSION['nick']}',
																				`birthday`		=	'{$_SESSION['bday']}',
																				`sex`			=	'{$_SESSION['sex']}',
																				`country`		=	'{$_SESSION['country']}',
																				`city`			=	'{$_SESSION['city']}',
																				`relationship`	=	'{$_SESSION['relationship']}'
																				WHERE `id`		=	'{$_SESSION['id']}'";
												if(!$query	=	@mysql_query($update))
													print 'There was an error updating your details';
													
												else
													print	'Your details were updated <a href=home.php>continue</a>';
											
											}
								}
							
						
					
					
			require	'inc/foot.inc';
		}
?>

an error is "Notice: Undefined index: sex in E:\programming\EasyPHP-5.3.6.1\www\ch@\profile.php on line 103

Notice: Undefined index: country in E:\programming\EasyPHP-5.3.6.1\www\ch@\profile.php on line 104

Notice: Undefined index: city in E:\programming\EasyPHP-5.3.6.1\www\ch@\profile.php on line 105

Notice: Undefined index: relationship in E:\programming\EasyPHP-5.3.6.1\www\ch@\profile.php on line 106"

Attachments: error.png
firemax Developer · 221 posts #2

That's mean $_POST['sex'], $_POST['country'] ..... not defined. in other words there no sex,country,city... details posted via form. if your not want to post that details via form simply remove lines 103,104,105,106 lines from code or you can hide this notices via adding 'display_errors(1)' under top of '<?php' tags.

<?php
    display_errors(1);
    require    'inc/head.inc'; 
    if(!isset($_SESSION['id'])) 
        header('Location:index.php'); 
    else 
        { 
            require 'inc/header.inc'; 
                 
                print    'Update profile or leave blank if you don\'t want to update'; 
                 
                $sql    =    "SELECT * FROM users WHERE id    =    '{$_SESSION['id']}'"; 
                $query    =    @mysql_query($sql, $con); 
                $result    =    @mysql_fetch_array($query); 
                         
                        if(isset($_SESSION['errors'])) 
                                if(is_array($_SESSION['errors'])) 
                                    foreach($_SESSION['errors'] as $error) 
                                        print    $error; 
                                         
                            if(!isset($_POST['count'])) 
                            { 
                                print    '<form action="" method=post> 
                                        <div class=dl>First Name</div> 
                                        <input type=text name=fname size=10 maxlength=30> 
                                        <div class=dl>Nick</div> 
                                        <input type=text name=nick size=10 maxlength=20> 
                                        <div class=dl>Last Name</div> 
                                        <input type=text name=sname size=10 maxlength=30> 
                                        <input type=hidden name=count> 
                                        <div class=dl>Gender</div> 
                                        <select name=sex> 
                                            <option value=m>Male</option> 
                                            <option value=f>Female</option> 
                                        </select><br> 
                                        <div class=dl>Country</div> 
                                        <input type=text name=country size=10 maxlength=30> 
                                        <div class=dl>City</div> 
                                        <input type=text name=city size=10 maxlength=30> 
                                        <div class=dl>Relationship</div> 
                                        <select name=relationship> 
                                            <option value="single">Single</option> 
                                            <option value="in a relationship">In a relationship</option> 
                                            <option value="engaged">Engaged</option> 
                                            <option value="married">Married</option> 
                                            <option value="available">Available</option> 
                                        </select><br> 
                                        <input class=button type=submit value=Update> 
                                        </form>'; 
                                         
                            } 
                             
                            else 
                                { 
                                    $_SESSION['fname']            =    @mysql_real_escape_string(htmlentities(stripslashes($_POST['fname']))); 
                                    $_SESSION['sname']            =    @mysql_real_escape_string(htmlentities(stripslashes($_POST['sname']))); 
                                    $_SESSION['nick']            =    @mysql_real_escape_string(htmlentities(stripslashes($_POST['nick']))); 
                                    $_SESSSION['sex']            =    @mysql_real_escape_string(htmlentities(stripslashes($_POST['sex']))); 
                                    $_SESSSION['country']        =    @mysql_real_escape_string(htmlentities(stripslashes($_POST['country']))); 
                                    $_SESSSION['city']            =    @mysql_real_escape_string(htmlentities(stripslashes($_POST['city']))); 
                                    $_SESSSION['relationship']    =    @mysql_real_escape_string(htmlentities(stripslashes($_POST['relationship']))); 
                                         
                                        $error    =    array(); 
                                        if(strlen($_SESSION['year']) != 4) 
                                            $error    =    '<br>Year must be a 4 digit'; 
                                        if(strlen($_SESSION['month']) != 2 || $_SESSION['month'] < 1 || $_SESSION['month'] > 12) 
                                            $error    =    '<br>Enter a valid month'; 
                                        if(strlen($_SESSION['day']) != 2 || $_SESSION['day'] < 1 || $_SESSION['day'] > 31) 
                                            $error    =    '<br>You entered an invalid day'; 
                                        if(count($error)) 
                                            { 
                                                        $_SESSION['error']     =    $error; 
                                                        if(is_array($_SESSION['error'])) 
                                                            { 
                                                                foreach($_SESSION['error'] as $errors) 
                                                                    print    $errors; 
                                                                     
                                                            } 
                                                        else 
                                                            print $_SESSION['error']; 
                                                            unset($_SESSION['error']); 
                                                            unset($_POST['count']); 
                                            } 
                                        else 
                                            { 
#Seems like my problem is below this comment 
                                                $update    =    "UPDATE users SET    `fname`            =    '{$_SESSION['fname']}', 
                                                                                `sname`            =    '{$_SESSION['sname']}', 
                                                                                `nick`            =    '{$_SESSION['nick']}', 
                                                                                `birthday`        =    '{$_SESSION['bday']}', 
                                                                                `sex`            =    '{$_SESSION['sex']}', 
                                                                                `country`        =    '{$_SESSION['country']}', 
                                                                                `city`            =    '{$_SESSION['city']}', 
                                                                                `relationship`    =    '{$_SESSION['relationship']}' 
                                                                                WHERE `id`        =    '{$_SESSION['id']}'"; 
                                                if(!$query    =    @mysql_query($update)) 
                                                    print 'There was an error updating your details'; 
                                                     
                                                else 
                                                    print    'Your details were updated <a href=home.php>continue</a>'; 
                                             
                                            } 
                                } 
                             
                         
                     
                     
            require    'inc/foot.inc'; 
        } 
?>

External image (may be gone) Free Mobile Web Scripts by me: Free Youtube Downloader, Tweets Reader, Facebook Wall Posts Reader PHP Tutorials: [How to Secure Your PHP Script (PHP SECURITY)](http://coding-talk.com/f14/how-to-secure-you…

arnage Lead · 811 posts #3

First $_POST into var to be (check) isset and than assign var into $_SESSION. And stop adding @ everywhere. 🙂

Easy way is to use ternary:

$var = isset($_POST['var'])/* if isset */ ? htmlentities($_POST['var'], ENT_QUOTES)/* condition true */ : ''/* condition false */;

Now var is defined even if it has no value ( '' ).

<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***
s3nzo Developer · 156 posts #4

Even if i do enter i $_POST['sex'], $_POST['country'], $_POST['city'] and $_POST['relationship'] are assigned i get the same error

Added after 22 minutes:

Oh sorry guys my problem is now solved i had wrong spellings at

$_SESSSION['sex']            =    @mysql_real_escape_string(htmlentities(stripslashes($_POST['sex'])));
                                    $_SESSSION['country']        =    @mysql_real_escape_string(htmlentities(stripslashes($_POST['country'])));
                                    $_SESSSION['city']            =    @mysql_real_escape_string(htmlentities(stripslashes($_POST['city'])));
                                    $_SESSSION['relationship']    =    @mysql_real_escape_string(htmlentities(stripslashes($_POST['relationship']))); 

There are 3 letter "S" instead of only 2 in $_SESSION

arnage Lead · 811 posts #5

U gotta be kidding... 🤣

<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***
s3nzo Developer · 156 posts #6

arnage wrote: U gotta be kidding... 🤣

Yeah bro i guess that's what happens if you are lazy to type the code and just copy and paste

Write your reply now; next you create a free account (or sign in) and it is posted right away. Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close