cougar23 Posted May 11, 2008 Share Posted May 11, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/105180-output-from-resulting-row/ Share on other sites More sharing options...
psychowolvesbane Posted May 11, 2008 Share Posted May 11, 2008 The only thing I can see is: $query = "select * from school_data where school_district = '".$selected_district."'"; should be: $query = "select * from school_data where school_district = '$selected_district'"; Quote Link to comment https://forums.phpfreaks.com/topic/105180-output-from-resulting-row/#findComment-538598 Share on other sites More sharing options...
Barand Posted May 12, 2008 Share Posted May 12, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/105180-output-from-resulting-row/#findComment-538606 Share on other sites More sharing options...
cougar23 Posted May 12, 2008 Author Share Posted May 12, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/105180-output-from-resulting-row/#findComment-538701 Share on other sites More sharing options...
LooieENG Posted May 12, 2008 Share Posted May 12, 2008 It's . in PHP, + is JavaScript I think Quote Link to comment https://forums.phpfreaks.com/topic/105180-output-from-resulting-row/#findComment-538708 Share on other sites More sharing options...
cougar23 Posted May 12, 2008 Author Share Posted May 12, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/105180-output-from-resulting-row/#findComment-538726 Share on other sites More sharing options...
cougar23 Posted May 12, 2008 Author Share Posted May 12, 2008 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>'; } } Quote Link to comment https://forums.phpfreaks.com/topic/105180-output-from-resulting-row/#findComment-538738 Share on other sites More sharing options...
sasa Posted May 12, 2008 Share Posted May 12, 2008 try print_r($row); to see is it formated right Quote Link to comment https://forums.phpfreaks.com/topic/105180-output-from-resulting-row/#findComment-538753 Share on other sites More sharing options...
cougar23 Posted May 13, 2008 Author Share Posted May 13, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/105180-output-from-resulting-row/#findComment-539644 Share on other sites More sharing options...
sasa Posted May 13, 2008 Share Posted May 13, 2008 mysqli commands isn't case sensitive, but keys of array is Quote Link to comment https://forums.phpfreaks.com/topic/105180-output-from-resulting-row/#findComment-539660 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.