Notice: Undefined index ERROR - Help!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Philth
    New Member
    • Oct 2007
    • 38

    Notice: Undefined index ERROR - Help!

    Hey there, it has to be said that I am a complete novice in PHP, I know this is a common error and has probably been covered to some degree already - but my head really does hurt - so I couldn't stand trawling through anymore forums hunting for the right answer. Anyway - here goes.

    The page I'm having trouble with is a simple edit row type form, which brings up the specific row content specified by the id(albumNumber) passed from the previous page, select.php. The page allows the user to edit, and then update the row displayed.


    The full error message...

    Notice: Undefined index: submit in /home/fhlinux162/g/greatestalbumse ver.com/user/htdocs/php/update.php on line 12

    The full php - taken and adapted from a tutorial I found...
    [code=php]
    <?

    // Connect database
    mysql_connect(" ","","");

    //select which database you want to edit
    mysql_select_db ("");

    // ***** This part will process when you Click on "Submit" button *****
    // Check, if you clicked "Submit" button
    if($_POST['submit'])
    {

    // Get parameters from form.
    $albumNumber=$_ POST['albumNumber'];
    $artistName=$_P OST['artistName'];
    $albumName=$_PO ST['albumName'];
    $albumInfo=$_PO ST['albumInfo'];
    $albumReview=$_ POST['albumReview'];

    // Do update statement.
    mysql_query("UP DATE Albums SET artistName='$ar tistName', albumName='$alb umName', albumInfo='$alb umInfo', albumReview='$a lbumReview' WHERE albumNumber='$a lbumNumber'");

    // Re-direct this page to select.php.
    echo "Query Finished";
    header("locatio n:select.php");


    }
    // ************* End update part *************

    // *** Select data to show on text fields in form. ***

    // Get id parameter (GET method) from select.php
    $albumNumber=$_ GET['albumNumber'];

    // Get records in all columns from table where column id equal in $id and put it in $result.
    $result=mysql_q uery("select * from Albums where albumNumber='$a lbumNumber'");

    // Split records in $result by table rows and put them in $row.
    $row=mysql_fetc h_assoc($result );

    // Close database connection.
    mysql_close();
    ?>

    <!-- END OF PHP CODES AND START HTML TAGS -->

    <html>
    <body>
    <!-- set this form to POST method and target this form to itself ($PHP_SELF;)-->
    <form id="form1" name="form1" method="post" action="update. php">
    <p>albumName : <br>
    <input name="albumName " type="text" id="albumName" value="<? echo $row['albumName']; ?>" size="40"/>
    <br />
    artistName :<br>
    <input name="artistNam e" type="text" id="artistName " value="<? echo $row['artistName']; ?>" size="40"/>
    <br />
    albumInfo : <br>
    <textarea name="albumInfo " cols="40" rows="4" id="albumInfo"> <? echo $row['albumInfo']; ?></textarea>
    <br />
    albumReview :<br>
    <textarea name="albumRevi ew" cols="40" rows="10" id="albumReview "><? echo $row['albumReview']; ?></textarea>
    <br />
    </p>
    <p>
    <input type="submit" name="Submit" value="Submit" />
    </p>
    </form>
    </body>
    </html>
    [/code]


    Any help would be much appreciated. The line if($_POST['submit']) work perfectly well in another scripts which adds new rows to the database.

    Thanks in advance.
    Last edited by pbmods; Oct 12 '07, 11:22 PM. Reason: Added CODE tags.
  • Motoma
    Recognized Expert Specialist
    • Jan 2007
    • 3236

    #2
    Originally posted by Philth
    Hey there, it has to be said that I am a complete novice in PHP, I know this is a common error and has probably been covered to some degree already - but my head really does hurt - so I couldn't stand trawling through anymore forums hunting for the right answer. Anyway - here goes.

    The page I'm having trouble with is a simple edit row type form, which brings up the specific row content specified by the id(albumNumber) passed from the previous page, select.php. The page allows the user to edit, and then update the row displayed.


    The full error message...

    Notice: Undefined index: submit in /home/fhlinux162/g/greatestalbumse ver.com/user/htdocs/php/update.php on line 12

    The full php - taken and adapted from a tutorial I found...

    <?

    // Connect database
    mysql_connect(" ","","");

    //select which database you want to edit
    mysql_select_db ("");

    // ***** This part will process when you Click on "Submit" button *****
    // Check, if you clicked "Submit" button
    if($_POST['submit'])
    {

    // Get parameters from form.
    $albumNumber=$_ POST['albumNumber'];
    $artistName=$_P OST['artistName'];
    $albumName=$_PO ST['albumName'];
    $albumInfo=$_PO ST['albumInfo'];
    $albumReview=$_ POST['albumReview'];

    // Do update statement.
    mysql_query("UP DATE Albums SET artistName='$ar tistName', albumName='$alb umName', albumInfo='$alb umInfo', albumReview='$a lbumReview' WHERE albumNumber='$a lbumNumber'");

    // Re-direct this page to select.php.
    echo "Query Finished";
    header("locatio n:select.php");


    }
    // ************* End update part *************

    // *** Select data to show on text fields in form. ***

    // Get id parameter (GET method) from select.php
    $albumNumber=$_ GET['albumNumber'];

    // Get records in all columns from table where column id equal in $id and put it in $result.
    $result=mysql_q uery("select * from Albums where albumNumber='$a lbumNumber'");

    // Split records in $result by table rows and put them in $row.
    $row=mysql_fetc h_assoc($result );

    // Close database connection.
    mysql_close();
    ?>

    <!-- END OF PHP CODES AND START HTML TAGS -->

    <html>
    <body>
    <!-- set this form to POST method and target this form to itself ($PHP_SELF;)-->
    <form id="form1" name="form1" method="post" action="update. php">
    <p>albumName : <br>
    <input name="albumName " type="text" id="albumName" value="<? echo $row['albumName']; ?>" size="40"/>
    <br />
    artistName :<br>
    <input name="artistNam e" type="text" id="artistName " value="<? echo $row['artistName']; ?>" size="40"/>
    <br />
    albumInfo : <br>
    <textarea name="albumInfo " cols="40" rows="4" id="albumInfo"> <? echo $row['albumInfo']; ?></textarea>
    <br />
    albumReview :<br>
    <textarea name="albumRevi ew" cols="40" rows="10" id="albumReview "><? echo $row['albumReview']; ?></textarea>
    <br />
    </p>
    <p>
    <input type="submit" name="Submit" value="Submit" />
    </p>
    </form>
    </body>
    </html>



    Any help would be much appreciated. The line if($_POST['submit']) work perfectly well in another scripts which adds new rows to the database.

    Thanks in advance.

    Indexes are case sensitive: in your HTML you use the name "Submit" however, in your $_POST you are asking for "submit" instead.

    Typically, checks on POST and GET variables are coupled with an isset in order to ensure no NOTICEs are sent in the (typically acceptable) cases where no data is sent to the page:

    [code=php]
    if(isset($_POST['Submit']) && $_POST['Submit'] == "Submit")
    [/code]

    In your case, being that there is only one potential value for Submit, you really only need to check isset().
    Last edited by Motoma; Oct 12 '07, 03:48 PM.

    Comment

    • Philth
      New Member
      • Oct 2007
      • 38

      #3
      Thanks for that!

      Seems to have done the job - no more error messages!

      Only thing now is that it doesn't actually update the row. Nothing happens. Hmmm!

      Comment

      • Motoma
        Recognized Expert Specialist
        • Jan 2007
        • 3236

        #4
        Originally posted by Philth
        Thanks for that!

        Seems to have done the job - no more error messages!

        Only thing now is that it doesn't actually update the row. Nothing happens. Hmmm!
        Take out the header() call to make sure that the MySQL query isn't giving you an error.

        Comment

        • Philth
          New Member
          • Oct 2007
          • 38

          #5
          Oh right, I see! New problem I think.

          Notice: Undefined index: albumNumber in /home/fhlinux162/g/greatestalbumse ver.com/user/htdocs/php/update.php on line 16


          Notice: Undefined index: albumNumber in /home/fhlinux162/g/greatestalbumse ver.com/user/htdocs/php/update.php on line 35

          Comment

          • Motoma
            Recognized Expert Specialist
            • Jan 2007
            • 3236

            #6
            Originally posted by Philth
            Oh right, I see! New problem I think.

            Notice: Undefined index: albumNumber in /home/fhlinux162/g/greatestalbumse ver.com/user/htdocs/php/update.php on line 16


            Notice: Undefined index: albumNumber in /home/fhlinux162/g/greatestalbumse ver.com/user/htdocs/php/update.php on line 35
            Same problem as before. Please read the error message before posting a question.

            Cypher:
            "Undefined index on Line 16"
            Translation:
            On line 16, you are using a variable as an array, and you are trying to look at an element in that array which does not exist.

            Comment

            • Philth
              New Member
              • Oct 2007
              • 38

              #7
              Originally posted by Motoma
              Same problem as before. Please read the error message before posting a question.

              Cypher:
              "Undefined index on Line 16"
              Translation:
              On line 16, you are using a variable as an array, and you are trying to look at an element in that array which does not exist.

              I had read the error report. Is is not the case that once this 'if' statement is read the variable in this array would exist? Please bare with me - I'm a completely ignoramous. The 'albumNumber' variable in line 16 has already been passed to this form and does exist. It's strange that this script is supplied by php tutorial websites when there are parts which are fundamentally wrong.

              Comment

              • Motoma
                Recognized Expert Specialist
                • Jan 2007
                • 3236

                #8
                Originally posted by Philth
                I had read the error report. Is is not the case that once this 'if' statement is read the variable in this array would exist? Please bare with me - I'm a completely ignoramous. The 'albumNumber' variable in line 16 has already been passed to this form and does exist. It's strange that this script is supplied by php tutorial websites when there are parts which are fundamentally wrong.
                I am running under the assumption that line 16 is:

                $albumNumber=$_ POST['albumNumber'];

                Take a look at your code to verify this.

                $albumNumber and $_POST are variables, 'albumNumber' is an index into the $_POST variable. In order for $_POST['albumNumber'] to have a value, there needs to be a field named 'albumNumber' in your HTML.

                Comment

                Working...