SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Dec 2004
    Location
    Alabama US
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Undefined Index on second use (Includes Code)

    This problem results in me modifying the "Jokes" example in the 2nd edition of BYO Database Driven Website (chapter 6.)

    The (modified) page displays the chosen question for editing, but when submitting the update this is returned:
    Notice: Undefined index: ID in c:\inetpub\wwwroot\omma\editquestion.php on line 77.

    This same "ID" was used to pull the info out of the database. I've screwed it up somewhere with updating the categories and submitter (I think.) Here is the link online:
    http://s93279249.onlinehome.us/questionlist.php
    Choose to "edit" a question, then submit. The above message doesn't display on the host site, only on my local setup.

    Here's the code:

    PHP Code:
    <?php
    if (isset($_POST['questiontext'])):
      
    // The question's details have been updated.
      
    $id $_POST['ID'];
      
    $questiontext $_POST['QuestionText'];
      
    $answertext $_POST['AnswerText'];
      
    $pageno $_POST['PageNo'];
      
    $comment $_POST['Comment'];
      
    $sid $_POST['SID'];
      
    $sql "UPDATE tblquestions SET
              QuestionText='
    $questiontext',
              AnswerText='
    $answertext',
              PageNo='
    $pageno',
              Comment='
    $comment',
              SID='
    $sid'
              WHERE ID='
    $id'";
      
    $sql "UPDATE tblquestioncategorylookup SET
              CID='
    $CID'
              WHERE ID=QID"
    ;
      if (
    mysql_query($sql)) {
        echo 
    '<p>Question details updated.</p>';
      } else {
        exit(
    '<p>Error updating question details: ' .
            
    mysql_error() . '</p>');
      }
      
    // Delete all existing entries for this
      // question from tblquestioncategorylookup
      
    $ok mysql_query("DELETE FROM tblquestioncategorylookup
                         WHERE QID='
    $id'");
      if (!
    $ok) {
        exit(
    '<p>Error removing question from all categories:' .
            
    mysql_error() . '</p>');
      }
      if (isset(
    $_POST['tblcategories'])) {
        
    $categories $_POST['tblcategories'];
      } else {
        
    $categories = array();
      }
      foreach (
    $categories as $catID) {
        
    $sql "INSERT IGNORE INTO tblquestioncategorylookup
                SET QID='
    $id', CID='$catID'";
        
    $ok = @mysql_query($sql);
        if (!
    $ok) {
          echo 
    "<p>Error inserting question into category $catID: " .
              
    mysql_error() . '</p>';
        }
      }
    ?>
    <p><a href="questions.php">New questions search</a></p>
    <?php else: // Allow the user to edit the question
    // **  Next line is the problem  **
    $id $_GET['ID'];
      
    $question = @mysql_query(
          
    "SELECT QuestionText, AnswerText, PageNo, Comment, SID FROM tblquestions WHERE ID='$id'");
      if (!
    $question) {
        exit(
    '<p>Error fetching question details: ' .
            
    mysql_error() . '</p>');
      }
      
    $question mysql_fetch_array($question);
      
    $questiontext $question['QuestionText'];
      
    $answertext $question['AnswerText'];
      
    $pageno $question['PageNo'];
      
    $comment $question['Comment'];
      
    $subid $question['SID'];
      
    // Convert HTML special characters
      // in database value for use in
      // an HTML document.
      
    $questiontext htmlspecialchars($questiontext);
      
    $answertext htmlspecialchars($answertext);
      
    $pageno htmlspecialchars($pageno);
      
    $comment htmlspecialchars($comment);
      
    // Get lists of submitters and categories for
      // the select box and checkboxes.
      
    $submitters = @mysql_query('SELECT ID as submitterid, FirstName, LastName FROM tblsubmitters');
      if (!
    $submitters) {
        exit(
    '<p>Unable to obtain submitter list from the database.</p>');
      }
      
    $categories = @mysql_query('SELECT ID as categoryid, Name FROM tblcategories');
      if (!
    $categories) {
        exit(
    '<p>Unable to obtain category list from the database.</p>');
      }
    ?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <p>Edit the question:<br />
    <textarea name="QuestionText" rows="10" cols="75">
    <?php echo $questiontext?></textarea>
    <p>Answer:<br />
    <textarea name="AnswerText">
    <?php echo $answertext?></textarea>
    <p>Page Number:<br />
    <textarea name="PageNo">
    <?php echo $pageno?></textarea>
    <p>Comment:<br />
    <textarea name="Comment">
    <?php echo $comment?></textarea>
    <p>Submitter:
    <select name="SID" size="3">
    <?php
      
    while ($submitter mysql_fetch_array($submitters)) {
        
    $sid $submitter['submitterid'];
        
    $afirstname htmlspecialchars($submitter['FirstName']);
        
    $alastname htmlspecialchars($submitter['LastName']);
        if (
    $sid == $subid) {
          echo 
    "<option selected='selected' value='$sid'>$afirstname $alastname</option>\n";
        } else {
          echo 
    "<option value='$sid'>$afirstname $alastname</option>\n";
        }
      }
    ?>
    </select></p>
    <p>In categories:<br />
    <?php
      
    while ($category mysql_fetch_array($categories)) {
        
    $cid $category['categoryid'];
        
    $cname htmlspecialchars($category['Name']);
        
    // Check if the question is in this category
        
    $result = @mysql_query(
          
    "SELECT * FROM tblquestioncategorylookup
           WHERE QID='
    $id' AND CID='$cid'");
        if (!
    $result) {
          exit(
    '<p>Error fetching question details: ' .
              
    mysql_error() . '</p>');
        }
        
    // mysql_num_rows gives the number of entries
        // in a result set. In this case, if the result
        // contains one or more rows, the condition
        // below will evaluate to true to indicate that
        // the question does belong to the category, and the
        // checkbox should be checked.
        
    if (mysql_num_rows($result)) {
          echo 
    "<input type='checkbox' checked='checked' name='tblcategories[]' value='$cid' />$cname<br />\n";
        } else {
          echo 
    "<input type='checkbox' name='tblcategories[]' value='$cid' />$cname<br />\n";
        }
      }
    ?>
    </p>
    <input type="hidden" name="ID" value="<?php echo $id?>" />
    <input type="submit" value="SUBMIT" />
    </form>
    <?php endif; ?>
    </body>
    </html>
    As far as I can tell, new questions and delete questions work fine.

    One week later, I'm at a complete loss. Thanks for any quidance.
    Last edited by frigginbone; Jun 11, 2005 at 06:15. Reason: [php] tags, no spaces, comment the error line
    frigginbone...as in, "please toss me a"

  2. #2
    SitePoint Wizard Dylan B's Avatar
    Join Date
    Jul 2004
    Location
    NYC
    Posts
    1,150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by frigginbone
    This problem results in me modifying the "Jokes" example in the 2nd edition of BYO Database Driven Website (chapter 6.)

    The (modified) page displays the chosen question for editing, but when submitting the update this is returned:
    Notice: Undefined index: ID in c:\inetpub\wwwroot\omma\editquestion.php on line 77.

    This same "ID" was used to pull the info out of the database. I've screwed it up somewhere with updating the categories and submitter (I think.) Here is the link online:
    http://s93279249.onlinehome.us/questionlist.php
    Choose to "edit" a question, then submit. The above message doesn't display on the host site, only on my local setup.

    Here's the code:

    Code:
    <?php
    
    if (isset($_POST['questiontext'])):
      // The question's details have been updated.
      
      $id = $_POST['ID'];
      $questiontext = $_POST['QuestionText'];
      $answertext = $_POST['AnswerText'];
      $pageno = $_POST['PageNo'];
      $comment = $_POST['Comment'];
      $sid = $_POST['SID'];
    
      $sql = "UPDATE tblquestions SET
              QuestionText='$questiontext',
              AnswerText='$answertext',
              PageNo='$pageno',
              Comment='$comment',
              SID='$sid'
              WHERE ID='$id'";
              
      $sql = "UPDATE tblquestioncategorylookup SET
              CID='$CID'
              WHERE ID=QID";
    
      if (mysql_query($sql)) {
        echo '<p>Question details updated.</p>';
      } else {
        exit('<p>Error updating question details: ' .
            mysql_error() . '</p>');
      }
    
      // Delete all existing entries for this
      // question from tblquestioncategorylookup
      $ok = mysql_query("DELETE FROM tblquestioncategorylookup
                         WHERE QID='$id'");
      if (!$ok) {
        exit('<p>Error removing question from all categories:' .
            mysql_error() . '</p>');
      }
    
      if (isset($_POST['tblcategories'])) {
        $categories = $_POST['tblcategories'];
      } else {
        $categories = array();
      }
    
      foreach ($categories as $catID) {
        $sql = "INSERT IGNORE INTO tblquestioncategorylookup
                SET QID='$id', CID='$catID'";
        $ok = @mysql_query($sql);
        if (!$ok) {
          echo "<p>Error inserting question into category $catID: " .
              mysql_error() . '</p>';
        }
      }
    
    ?>
    
    <p><a href="questions.php">New questions search</a></p>
    
    <?php else: // Allow the user to edit the question
    
      $id = $_GET['ID'];
    
      $question = @mysql_query(
          "SELECT QuestionText, AnswerText, PageNo, Comment, SID FROM tblquestions WHERE ID='$id'");
      if (!$question) {
        exit('<p>Error fetching question details: ' .
            mysql_error() . '</p>');
      }
    
      $question = mysql_fetch_array($question);
    
      $questiontext = $question['QuestionText'];
      $answertext = $question['AnswerText'];
      $pageno = $question['PageNo'];
      $comment = $question['Comment'];
      $subid = $question['SID'];
    
      // Convert HTML special characters
      // in database value for use in
      // an HTML document.
      $questiontext = htmlspecialchars($questiontext);
      $answertext = htmlspecialchars($answertext);
      $pageno = htmlspecialchars($pageno);
      $comment = htmlspecialchars($comment);
    
      // Get lists of submitters and categories for
      // the select box and checkboxes.
      $submitters = @mysql_query('SELECT ID as submitterid, FirstName, LastName FROM tblsubmitters');
      if (!$submitters) {
        exit('<p>Unable to obtain submitter list from the database.</p>');
      }
    
      $categories = @mysql_query('SELECT ID as categoryid, Name FROM tblcategories');
      if (!$categories) {
        exit('<p>Unable to obtain category list from the database.</p>');
      }
    ?>
    
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <p>Edit the question:<br />
    <textarea name="QuestionText" rows="10" cols="75">
    <?php echo $questiontext; ?></textarea>
    <p>Answer:<br />
    <textarea name="AnswerText">
    <?php echo $answertext; ?></textarea>
    <p>Page Number:<br />
    <textarea name="PageNo">
    <?php echo $pageno; ?></textarea>
    <p>Comment:<br />
    <textarea name="Comment">
    <?php echo $comment; ?></textarea>
    <p>Submitter:
    <select name="SID" size="3">
    <?php
      while ($submitter = mysql_fetch_array($submitters)) {
        $sid = $submitter['submitterid'];
        $afirstname = htmlspecialchars($submitter['FirstName']);
        $alastname = htmlspecialchars($submitter['LastName']);
        if ($sid == $subid) {
          echo "<option selected='selected' value='$sid'>$afirstname $alastname</option>\n";
        } else {
          echo "<option value='$sid'>$afirstname $alastname</option>\n";
        }
      }
    ?>
    </select></p>
    <p>In categories:<br />
    <?php
      while ($category = mysql_fetch_array($categories)) {
        $cid = $category['categoryid'];
        $cname = htmlspecialchars($category['Name']);
    
        // Check if the question is in this category
        $result = @mysql_query(
          "SELECT * FROM tblquestioncategorylookup
           WHERE QID='$id' AND CID='$cid'");
        if (!$result) {
          exit('<p>Error fetching question details: ' .
              mysql_error() . '</p>');
        }
    
        // mysql_num_rows gives the number of entries
        // in a result set. In this case, if the result
        // contains one or more rows, the condition
        // below will evaluate to true to indicate that
        // the question does belong to the category, and the
        // checkbox should be checked.
        if (mysql_num_rows($result)) {
          echo "<input type='checkbox' checked='checked' name='tblcategories[]' value='$cid' />$cname<br />\n";
        } else {
          echo "<input type='checkbox' name='tblcategories[]' value='$cid' />$cname<br />\n";
        }
      }
    ?>
    </p>
    <input type="hidden" name="ID" value="<?php echo $id; ?>" />
    <input type="submit" value="SUBMIT" />
    </form>
    
    <?php endif; ?>
    
    </body>
    </html>
    As far as I can tell, new questions and delete questions work fine.

    One week later, I'm at a complete loss. Thanks for any quidance.
    Before I even look at the code, put it in [ php ] and [/ php ] tags, but without the spaces, and comment the line with the error.

  3. #3
    SitePoint Member
    Join Date
    Dec 2004
    Location
    Alabama US
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your time, Dylan B. I think I've got the code the way you need it now.
    frigginbone...as in, "please toss me a"

  4. #4
    SitePoint Wizard Dylan B's Avatar
    Join Date
    Jul 2004
    Location
    NYC
    Posts
    1,150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now which is line 77

  5. #5
    SitePoint Wizard Young Twig's Avatar
    Join Date
    Dec 2003
    Location
    Albany, New York
    Posts
    1,355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is a comment that says, "Next line is the problem."

    The problem is that PHP_SELF doesn't include the query string (?id=X). You can do one of the following two things:

    1. Change $_SERVER['PHP_SELF'] to $_SERVER['REQUEST_URI'] in your form.
    2. Change $_GET['ID'] to $_POST['ID'] on line 77.

  6. #6
    SitePoint Member
    Join Date
    Dec 2004
    Location
    Alabama US
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Young Twig
    There is a comment that says, "Next line is the problem."

    The problem is that PHP_SELF doesn't include the query string (?id=X). You can do one of the following two things:

    1. Change $_SERVER['PHP_SELF'] to $_SERVER['REQUEST_URI'] in your form.
    2. Change $_GET['ID'] to $_POST['ID'] on line 77.
    Suggestion#1: The database info is retrieved for edit, then returns "page cannot be displayed" when changes are submitted.

    Suggestion#2: The database info is NOT retrieved for edit, and the same "Notice: Undefined index: ID in c:\inetpub\wwwroot\omma\editquestion.php on line 77" is given.

    Thanks for your help, Young Twig. Any other suggestions?
    frigginbone...as in, "please toss me a"

  7. #7
    SitePoint Wizard Dylan B's Avatar
    Join Date
    Jul 2004
    Location
    NYC
    Posts
    1,150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Young Twig
    There is a comment that says, "Next line is the problem."

    The problem is that PHP_SELF doesn't include the query string (?id=X). You can do one of the following two things:

    1. Change $_SERVER['PHP_SELF'] to $_SERVER['REQUEST_URI'] in your form.
    2. Change $_GET['ID'] to $_POST['ID'] on line 77.

    Sorry, I am tres blind


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •