O'Reilly Forums: Chapter 8 Questionnaire Not Populating - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Chapter 8 Questionnaire Not Populating

#1 User is offline   maryjane9110024 

  • New Member
  • Pip
  • Group: Members
  • Posts: 5
  • Joined: 20-July 11

Posted 16 June 2014 - 09:58 AM

I am at page 458 in chapter 8 trying to test drive the questionnaire page. I have been trying to figure out why this table is not populating. I added in some or die() to the mysqli_query()'s and one issue is there is an error for "category_id" column. I went back and tried to see if I missed something but I haven't. There is a category column but no category_id column.

I have copied and pasted the file thinking maybe I have a typo somewhere but it is doing the same thing. Now its erroring out on the first query:
$query = "SELECT topic_id FROM mismatch_topic ORDER BY category_id, topic_id";


Here's the code for the questionnaire page. EXACTLY as it is in the book (but with added die statements).

<?php
  // Start the session
  require_once('startsession.php');

  // Insert the page header
  $page_title = 'Questionnaire';
  require_once('header.php');

  require_once('appvars.php');
  require_once('connectvars.php');

  // Make sure the user is logged in before going any further.
  if (!isset($_SESSION['user_id'])) {
    echo '<p class="login">Please <a href="login.php">log in</a> to access this page.</p>';
    exit();
  }

  // Show the navigation menu
  require_once('navmenu.php');

  // Connect to the database
  $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

  // If this user has never answered the questionnaire, insert empty responses into the database
  $query = "SELECT * FROM mismatch_response WHERE user_id = '" . $_SESSION['user_id'] . "'";
  $data = mysqli_query($dbc, $query) or die("Error 1: ".mysqli_error($dbc));
  if (mysqli_num_rows($data) == 0) {
    // First grab the list of topic IDs from the topic table
    $query = "SELECT topic_id FROM mismatch_topic ORDER BY category_id, topic_id";
    $data = mysqli_query($dbc, $query) or die("Error 2: ".mysqli_error($dbc));
    $topicIDs = array();
    while ($row = mysqli_fetch_array($data)) {
      array_push($topicIDs, $row['topic_id']);
    }

    // Insert empty response rows into the response table, one per topic
    foreach ($topicIDs as $topic_id) {
      $query = "INSERT INTO mismatch_response (user_id, topic_id) VALUES ('" . $_SESSION['user_id']. "', '$topic_id')";
      mysqli_query($dbc, $query) or die("Error 3: ".mysqli_error($dbc));
    }
  }

  // If the questionnaire form has been submitted, write the form responses to the database
  if (isset($_POST['submit'])) {
    // Write the questionnaire response rows to the response table
    foreach ($_POST as $response_id => $response) {
      $query = "UPDATE mismatch_response SET response = '$response' WHERE response_id = '$response_id'";
      mysqli_query($dbc, $query) or die("Error 4: ".mysqli_error($dbc));
    }
    echo '<p>Your responses have been saved.</p>';
  }

  // Grab the response data from the database to generate the form
  $query = "SELECT response_id, topic_id, response FROM mismatch_response WHERE user_id = '" . $_SESSION['user_id'] . "'";
  $data = mysqli_query($dbc, $query) or die("Error 5: ".mysqli_error($dbc));
  $responses = array();
  while ($row = mysqli_fetch_array($data)) {
    // Look up the topic name for the response from the topic table
    $query2 = "SELECT name, category FROM mismatch_topic WHERE topic_id = '" . $row['topic_id'] . "'";
    $data2 = mysqli_query($dbc, $query2) or die("Error 6: ".mysqli_error($dbc));
    if (mysqli_num_rows($data2) == 1) {
      $row2 = mysqli_fetch_array($data2);
      $row['topic_name'] = $row2['name'];
      $row['category_name'] = $row2['category'];
      array_push($responses, $row);
    }
  }

  mysqli_close($dbc);

  // Generate the questionnaire form by looping through the response array
  echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">';
  echo '<p>How do you feel about each topic?</p>';
  $category = $responses[0]['category_name'];
  echo '<fieldset><legend>' . $responses[0]['category_name'] . '</legend>';
  foreach ($responses as $response) {
    // Only start a new fieldset if the category has changed
    if ($category != $response['category_name']) {
      $category = $response['category_name'];
      echo '</fieldset><fieldset><legend>' . $response['category_name'] . '</legend>';
    }

    // Display the topic form field
    echo '<label ' . ($response['response'] == NULL ? 'class="error"' : '') . ' for="' . $response['response_id'] . '">' . $response['topic_name'] . ':</label>';
    echo '<input type="radio" id="' . $response['response_id'] . '" name="' . $response['response_id'] . '" value="1" ' . ($response['response'] == 1 ? 'checked="checked"' : '') . ' />Love ';
    echo '<input type="radio" id="' . $response['response_id'] . '" name="' . $response['response_id'] . '" value="2" ' . ($response['response'] == 2 ? 'checked="checked"' : '') . ' />Hate<br />';
  }
  echo '</fieldset>';
  echo '<input type="submit" value="Save Questionnaire" name="submit" />';
  echo '</form>';

  // Insert the page footer
  require_once('footer.php');
?>


This post has been edited by maryjane9110024: 16 June 2014 - 11:51 AM

0

#2 User is offline   maryjane9110024 

  • New Member
  • Pip
  • Group: Members
  • Posts: 5
  • Joined: 20-July 11

Posted 16 June 2014 - 11:53 AM

View Postmaryjane9110024, on 16 June 2014 - 09:58 AM, said:

I am at page 458 in chapter 8 trying to test drive the questionnaire page. I have been trying to figure out why this table is not populating. I added in some or die() to the mysqli_query()'s and one issue is there is an error for "category_id" column. I went back and tried to see if I missed something but I haven't. There is a category column but no category_id column.

I have copied and pasted the file thinking maybe I have a typo somewhere but it is doing the same thing. Now its erroring out on the first query:
$query = "SELECT topic_id FROM mismatch_topic ORDER BY category_id, topic_id";




It seems there is an error in the script. The first query (shown above) should actually be the following:
$query = "SELECT topic_id FROM mismatch_topic ORDER BY category, topic_id";


It works with that small change.
0

#3 User is offline   lemuel 

  • Super Veteran Member
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: Members
  • Posts: 571
  • Joined: 13-April 09

Posted 16 June 2014 - 03:25 PM

View Postmaryjane9110024, on 16 June 2014 - 11:53 AM, said:

It seems there is an error in the script. The first query (shown above) should actually be the following:
$query = "SELECT topic_id FROM mismatch_topic ORDER BY category, topic_id";


It works with that small change.


Listed in errata
http://www.oreilly.c...n=9780596006303
0

#4 User is offline   maryjane9110024 

  • New Member
  • Pip
  • Group: Members
  • Posts: 5
  • Joined: 20-July 11

Posted 17 June 2014 - 06:26 AM

View Postlemuel, on 16 June 2014 - 03:25 PM, said:



Thank you for that list!! I didn't even know that existed. :D
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users