Jump to content

output from resulting row


cougar23

Recommended Posts

I'm having a problem understanding why this code is throwing an error, I have two places I use almost identical code with a different query, the first works and the second throws an "undefined index" when i go to output it from the result set row.

 

This code works and populates my drop-down select box with the correct information

//otherwise build & run query to get result set
$query = "select distinct school_district from school_data order by school_district";
$results = db_query('guest', 'guest', $query);

$num_results = $results -> num_rows;

for($i=0; $i < $num_results; $i++) {

	$row = $results -> fetch_assoc();
	echo '<option value="'.$row['school_district'].'">'.$row['school_district'].'</option>';
}

 

When my form is submitted I want to retrieve the information from the database that goes with the selected school district and build a row in my HTML output table (where this code is being included).

 

$selected_district = $_POST['schoolDistricts'];
	$query = "select * from school_data where school_district = '".$selected_district."'";
	$results = db_query('guest', 'guest', $query);

	//otherwise loop through results and output information
	$num_results = $results -> num_rows;
	echo $num_results;

	for($i=0; $i < $num_results; $i++) {

		$row = $results -> fetch_assoc();
		echo '<tr>'.
				'<td>'.$row['school_name'].'</td>'.
				'<td>'.$row['school_district'].'</td>'.
				'<td>'.$row['city'].'</td>'.
				'<td>'.$row['state'].'</td>'.
				'<td>'.$row['mascot'].'</td>'.
				'<td>'.$row['school_colors'].'</td>'.
			'</tr>';
	}

 

I'm not understanding why the "Undefined index" error when trying to pull out the data in the corresponding column, when it worked fine when populating the select box?  Am i missing something really easy about pulling column data from a row and outputting it to a page?

 

 

Link to comment
Share on other sites

You don't give any clues about where the error occurs so I'll have be general.

 

You get an "undefined index" error if you try to reference an array index that doesn't exist.

 

So if $_POST['schoolDistricts'] doesn't exist, maybe because no data has yet been posted, then you get the error.

 

 

 

@psychowolvesbane,

 

and resulting difference between those two methods is what?

Link to comment
Share on other sites

I apologize for not being clear.  I've altered my code as below.  It would appear the problem is that its not getting the values of $selected_school/$selected_district.  The POST['searchType'] is a form radio button group of two buttons to distinguish whether the user has selected to search by school name or school district.  I have hidden variables that get set via Javascript in the form, and that is what the 'selectedSchool' and 'selectedDistrict' POST variables are. I'm wanting to take the appropriate variable's value and plug it into my query, and then print a <tr> of all the database columns associated with the query result set.  It looks like I'm not getting the value from whichever hidden form variable (selectedSchool/selectedDistrict), at least that's my guess as to why the error. 

 

The error I'm getting is "Notice: Undefined index: selectedSchool in C:\Apache2.2\htdocs\sportsdata\scripts\get_school_data.php on line 18"

 

//else test to see which type of search was specified and set the query accordingly
if(isset($_POST['searchType'])) { 

	//determine the syntax of the query based on search type selected
	$selected_radio = $_POST['searchType'];

	if ($selected_radio == 'searchByName') {

		$selected_school = $_POST['selectedSchool'];
		echo $selected_school + '</br>';
		$query = "select * from school_data where school_name = '$selected_school'";
		echo $query + '</br>';
	}	

	else if ($selected_radio == 'searchByDistrict') {

		$selected_district = $_POST['selectedDistrict'];
		echo $selected_district + '</br>';
		$query = "select * from school_data where school_name = '$selected_district'";
		echo $query + '</br>';
	}

 

Hopefully this is more clear about my problem; thanks for all your help.

Link to comment
Share on other sites

yes that's correct i noticed that after my last post; however, I dont think that's the issue as i'm still getting the problem having replacted the + signs with dots.  The issue appears to be not correctly getting the value from the hidden variable (selectedSchool/selectedDistrict).  I put in

 

echo $selectedSchool and nothing is output (even with a school selected in my drop-down select

 

also put in echo $query and the what got output was

 

select * school school_data where school_name = "

 

and that's it no value and no closed quote, and that should also be a single quote i believe quoting the value

Link to comment
Share on other sites

I figured out the getting the values from the combo boxes directly and am now longer using the hidden variables on the form, I'm pulling the selected value out of the select box directly.  The query is now formatted correctly, and I'm also returning the correcty number of resulting rows (verified via the echo $num_rows), however, I'm still getting an "Undefined index" error on each of the $row[attribute] output calls, which puts me close to where i stared originally with this page.  What am I doing wrong it outputting each column's data from the result set's rows?

 


//if a search type was selected, else test to see which type of search 
//was specified and set the query accordingly
if(isset($_POST['searchType'])) { 

	//determine the syntax of the query based on search type selected
	$selected_radio = $_POST['searchType'];

	if ($selected_radio == 'searchByName')
		$query = "select * from school_data where school_name = '".$_POST['schoolNames']."'";

	else if ($selected_radio == 'searchByDistrict')
		$query = "select * from school_data where school_district = '".$_POST['schoolDistricts']."'";

	//execute the appropriate query to obtain a result set
	echo $query;
	$results = db_query('guest', 'guest', $query);

	//otherwise loop through results and output information
	$num_results = $results -> num_rows;
	echo $num_results;

	for($i=0; $i < $num_results; $i++) {

		$row = $results -> fetch_assoc();
		echo '<tr>'.
				'<td>'.$row['school_name'].'</td>'.
				'<td>'.$row['school_district'].'</td>'.
				'<td>'.$row['city'].'</td>'.
				'<td>'.$row['state'].'</td>'.
				'<td>'.$row['mascot'].'</td>'.
				'<td>'.$row['school_colors'].'</td>'.
			'</tr>';
	}
}

Link to comment
Share on other sites

After using print_r($row) i verified the result set is correct.  I tried changing each attribute to caps, i.e. $row['SCHOOL_NAME']; and the code now works.  I didn't thing the mysqli commands were case sensitive, but I gues that a newbie mistake :) Thanks to all who hav replied and helped with solving the issue.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.