How to fix "Notice : undefined index"

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    How to fix "Notice : undefined index"

    Hey guys i'm getting an error using this code to update user details in a database
    PHP Code:
    <?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']) != || $_SESSION['month'] < || $_SESSION['month'] > 12)
                                                
    $error    =    '<br>Enter a valid month';
                                            if(
    strlen($_SESSION['day']) != || $_SESSION['day'] < || $_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"
    Attached Files
    libra.wen.ru

    #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.

    Code:
    <?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'; 
            } 
    ?>

    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)
    Want to Develop/Edit your WAP/Web Site? Add me to Gtalk (gmail) 'lakshan1989' or PM me.

    Comment


      #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:

      PHP Code:
      $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 ( '' ).
      Last edited by arnage; 07.09.11, 19:20.
      <!DOCTYPE html PUBLIC "-//WAPFORUM.RS

      Comment


        #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
        PHP Code:
        $_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
        Last edited by s3nzo; 07.09.11, 21:59.
        libra.wen.ru

        Comment


          #5
          U gotta be kidding...
          <!DOCTYPE html PUBLIC "-//WAPFORUM.RS

          Comment


            #6
            Originally posted by arnage View Post
            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
            libra.wen.ru

            Comment

            Working...
            X