Foros del Web » Programando para Internet » PHP »

Problemas con un cuestionario

Estas en el tema de Problemas con un cuestionario en el foro de PHP en Foros del Web. Estoy haciendo un cuestionario, parece funcionar todo bien, pero en esta línea Código: while ($row = mysqli_fetch_array($data)) me tira el siguiente error Warning: mysqli_fetch_array() expects ...
  #1 (permalink)  
Antiguo 04/10/2012, 17:14
 
Fecha de Ingreso: septiembre-2010
Mensajes: 6
Antigüedad: 13 años, 7 meses
Puntos: 0
Exclamación Problemas con un cuestionario

Estoy haciendo un cuestionario, parece funcionar todo bien, pero en esta línea

Código:
while ($row = mysqli_fetch_array($data))
me tira el siguiente error

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\modulo09\questionnaire.php on line 32

Alguien me puede decir como solucionarlo??
  #2 (permalink)  
Antiguo 04/10/2012, 17:32
Avatar de kendall00  
Fecha de Ingreso: septiembre-2011
Ubicación: $Ubicacion => ['Costa Rica'];
Mensajes: 85
Antigüedad: 12 años, 7 meses
Puntos: 5
Respuesta: Problemas con un cuestionario

Todo el codigo por favor!!!
__________________
El limite es la imaginacion... con Dios todo es posible.
  #3 (permalink)  
Antiguo 04/10/2012, 19:35
Avatar de rodrigo791  
Fecha de Ingreso: noviembre-2009
Ubicación: Uruguay
Mensajes: 1.339
Antigüedad: 14 años, 5 meses
Puntos: 168
Respuesta: Problemas con un cuestionario

El error lo dice todo, mysqli_fetch_array esperaba 1 parametro, hace echo var_dump($data) a ver que pasa ahí, seguramente sea null su valor.
  #4 (permalink)  
Antiguo 08/10/2012, 21:40
 
Fecha de Ingreso: septiembre-2010
Mensajes: 6
Antigüedad: 13 años, 7 meses
Puntos: 0
Respuesta: Problemas con un cuestionario

Cita:
Iniciado por kendall00 Ver Mensaje
Todo el codigo por favor!!!

Código:
<?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 response into the database

$query = "SELECT * FROM mismatch_response 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
	$query = "SELECT topic_id FROM mismatch_topic ORDER BY category_id, 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){
		$query = "INSERT INTO mismatch_response(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){
		$query = "UPDATE mismatch_response SET response = '$response' " . 
		"WHERE response_id = '$response_id'";
		mysqli_query($dbc, $query);
	}
	echo '<p>Your responses hace been saved.</p>';
	
	//Grab the responses 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);
	$response = 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);
		if(mysqli_num_rows($data2) == 1){
			$row2 = mysqli_fetch_array($data2);
			$row['topic_name'] = $row2['name'];
			$row['category_name'] = $row2['category'];
			array_push($response, $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>' . $response [0]  ['category_name'] . '</legend>';
	foreach($response 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');
		?>
  #5 (permalink)  
Antiguo 08/10/2012, 21:57
Avatar de rodrigo791  
Fecha de Ingreso: noviembre-2009
Ubicación: Uruguay
Mensajes: 1.339
Antigüedad: 14 años, 5 meses
Puntos: 168
Respuesta: Problemas con un cuestionario

como dije es claro el error, hace echo var_dump($data) a ver que valor tiene, al parecer no le llega nada a fetch_array
  #6 (permalink)  
Antiguo 09/10/2012, 07:48
 
Fecha de Ingreso: septiembre-2010
Mensajes: 6
Antigüedad: 13 años, 7 meses
Puntos: 0
Respuesta: Problemas con un cuestionario

En que parte del código pongo esa línea?
  #7 (permalink)  
Antiguo 09/10/2012, 08:43
Avatar de Javier01  
Fecha de Ingreso: febrero-2008
Ubicación: Montevideo
Mensajes: 261
Antigüedad: 16 años, 2 meses
Puntos: 31
Respuesta: Problemas con un cuestionario

deberias pegarlo bajo
Código PHP:
Ver original
  1. $data = mysqli_query($dbc, $query);
antes que de el error.
Tambien podrias realizar la consulta anterior al error directamente en tu BD. Seguramente devuelva vacio, o este mal la consulta.

Saludos
__________________
Tomarse un tiempo para redactar correctamente la pregunta, utilizando los signos de puntuación adecuados, es ganar tiempo y calidad en la respuesta.

Etiquetas: falla, formulario
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 09:15.