light87 Posted December 16, 2009 Share Posted December 16, 2009 Hello: I'm a newbie to PHP and MySQL and have a problem with one of my files on my Website. It was working properly before I started to condense the coding using JOINS, etc. Please note that for my naming of the Tables: mismatch_topic = peach mismatch_response = leach mismatch_user = Beach mismatch_category = keach I am able to login to the mismatch Website that I created, however when I click on the questionnaire, I receive the following error message: Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /home/content/PRIVATE/html/questionnaire_Chapter8_Page457.php on line 138 I would appreciate the help!!! It's probably not necessary to list all of the files that are on my Website, so I am listing only the file that I think is causing the problems. <? ob_start(); ?> <?php // Filename: C:,questionnaire_Chapter8_Page481.php // Start the session require_once('startsession_Chapter8_Page457.php'); // Insert the page header $page_title = 'Questionnaire'; require_once('header_Chapter8_Page457.php'); require_once('appvars_Chapter8_Page457.php'); require_once('connectvars_Chapter8_Page457.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_Chapter8_Page457.php">log in</a> to access this page.</p>'; exit(); } // Show the navigation menu require_once('navmenu_Chapter8_Page457.php'); // Connect to the database $dbc = mysqli_connect('PRIVATE', 'PRIVATE', 'PRIVATE', 'PRIVATE'); // If this user has never answered the questionnaire, insert empty responses into the database // changed mismatch_response to leach on 11,18,09 $query = "SELECT * FROM leach WHERE user_id = '" . $_SESSION['user_id'] . "'"; $data = mysqli_query($dbc, $query); if (mysqli_num_rows($data) == 0) { // First grab the list of topic IDs from the topic table //changed category_id to category per the errata on 11\10\09 // changed mismatch_topic to peach on 11,18,09 $query = "SELECT topic_id FROM peach ORDER BY category, topic_id"; $data = mysqli_query($dbc, $query); $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) { // changed mismatch_response to leach on 11,18,09 $query = "INSERT INTO leach (user_id, topic_id) VALUES ('" . $_SESSION['user_id']. "', '$topic_id')"; mysqli_query($dbc, $query); } } // 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) { // changed mismatch_response to leach on 11,18,09 $query = "UPDATE leach SET response = '$response' WHERE response_id = '$response_id'"; mysqli_query($dbc, $query); } echo '<p>Your responses have been saved.</p>'; } // Grab the response data from the database to generate the form // changed mismatch_response to leach on 11,18,09 // BEGINNING of insert on 12,15,09 // This coding was inserted for Chapter 8, page 480 // First grab the user's responses from the response table (JOIN to get the topic name) $query = "SELECT mr.response_id, mr.topic_id, mr.response," . "mt.name AS topic_name, mc.name AS category_name" . "FROM leach AS mr" . "INNER JOIN peach AS mt USING (topic_id) " . "INNER JOIN keach AS mc USING (category_id) " . "WHERE mr.user_id = '" . $_SESSION['user_id'] . "'"; $data = mysqli_query($dbc, $query); $responses = array(); while ($row = mysqli_fetch_array($data)) { array_push($user_responses, $row); } // END of insert on 12,15,09 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_Chapter8_Page457.php'); ?> <? ob_flush(); ?> Link to comment https://forums.phpfreaks.com/topic/185384-php-and-mysql-help/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.