ichversuchte Posted May 2, 2006 Share Posted May 2, 2006 I am trying to populate a combo box, but i keep getting this error - "Query failed. " in my browser.This is what the code looks like[code]<?phpinclude_once('Scripts/conn/cid.php'); $sql = "SELECT ra_name FROM staff_elkin"; $result = mysql_query($sql) or die('Query failed. ' . mysql_error());if (mysql_num_rows($result) == 1) { while ($row = mysql_fetch_array($result)) { $title = $row['title']; ?> <select name="raname" id="raname"> <?php echo("<option value='$title'>$title</option>"); ?> </select> <?php }} include_once('Scripts/close/end.php');?>[/code]This is what the connection to the databse looks like[code]<?php $usr = "xxxxxx"; $pwd = "xxxxxxx"; $db = "xxxxx"; $host = "localhost"; # connect to database $cid = mysql_connect($host,$usr,$pwd); if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); }?>anyone have any idea Quote Link to comment https://forums.phpfreaks.com/topic/8913-help-populating-combo-box/ Share on other sites More sharing options...
sasa Posted May 2, 2006 Share Posted May 2, 2006 inser[code]$xxxx=mysql_select_db($db);[/code]in your code Quote Link to comment https://forums.phpfreaks.com/topic/8913-help-populating-combo-box/#findComment-32736 Share on other sites More sharing options...
ichversuchte Posted May 2, 2006 Author Share Posted May 2, 2006 This is what i have now in the code and the error is gone but a combo box doesn't appear and there is def. people in this table.THIS IS THE CODE OR UPLOAD.PHP[code]<?phpinclude_once('Scripts/conn/cid.php'); $sel=mysql_select_db($db);$sql = "SELECT ra_name FROM staff_elkin"; $result = mysql_query($sql) or die('Query failed. ' . mysql_error());if (mysql_num_rows($result) == 1) { while ($row = mysql_fetch_array($result)) { $title = $row['title']; ?> <select name="raname" id="raname"> <?php echo("<option value='$title'>$title</option>"); ?> </select> <?php }} include_once('Scripts/close/end.php');?>[/code]THIS IS THE CODE FOR THIS FILE - include_once('Scripts/conn/cid.php');[code]<?php $usr = ""; $pwd = ""; $db = "forthest_intranet"; $host = "localhost"; # connect to database $cid = mysql_connect($host,$usr,$pwd); mysql_select_db($db); if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); }?>and now i don't get anything[/code] Quote Link to comment https://forums.phpfreaks.com/topic/8913-help-populating-combo-box/#findComment-32748 Share on other sites More sharing options...
.josh Posted May 2, 2006 Share Posted May 2, 2006 shouldn't this:if (mysql_num_rows($result) == 1){be this:if (mysql_num_rows($result) > 0){the way you have it now, you are only executing the stuff inside your if statement if there is only 1 result from the query. Quote Link to comment https://forums.phpfreaks.com/topic/8913-help-populating-combo-box/#findComment-32757 Share on other sites More sharing options...
ichversuchte Posted May 2, 2006 Author Share Posted May 2, 2006 This fixed the problem with showing output, I can now see combo box but its printing out two of them with nothing in them.I have the page here - [a href=\"http://ohrl.forthestudentsbookstore.com/Elkin/upload.php\" target=\"_blank\"]http://ohrl.forthestudentsbookstore.com/Elkin/upload.php[/a]I wrote this sql state to make sure there is something there...which you can there is something therehere the code revised....half of it is the test query[code]<?phpinclude_once('Scripts/conn/cid.php'); $sql = "SELECT ra_name FROM staff_elkin"; $result = mysql_query($sql) or die('Query failed. ' . mysql_error());if (mysql_num_rows($result) > 0) { while ($row = mysql_fetch_array($result)) { $title = $row['title']; ?> <select name="raname" id="raname"> <?php echo("<option value='$title'>$title</option>"); ?> </select> <?php }} include_once('Scripts/close/end.php');?>//THIS IS THE QUERY CODE TO TEST THE DATABASE<?phpinclude_once('Scripts/conn/conn.php'); $rowsPerPage = 300;$pageNum = 1;if(isset($_GET['page'])){$pageNum = $_GET['page'];}$offset = ($pageNum - 1) * $rowsPerPage;$query = "SELECT ra_name FROM staff_elkin"; $result = mysql_query($query) or die('Error, query failed');echo '<table border="0">';while(list($raname) = mysql_fetch_array($result)){echo "<tr><td>$raname <em>made the request:</em></td></tr>";}echo '</table>'; echo '<br>';include_once('Scripts/close/end.php');?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/8913-help-populating-combo-box/#findComment-32766 Share on other sites More sharing options...
sasa Posted May 2, 2006 Share Posted May 2, 2006 [!--quoteo(post=370708:date=May 2 2006, 11:39 PM:name=ichversuchte)--][div class=\'quotetop\']QUOTE(ichversuchte @ May 2 2006, 11:39 PM) [snapback]370708[/snapback][/div][div class=\'quotemain\'][!--quotec--]This fixed the problem with showing output, I can now see combo box but its printing out two of them with nothing in them.I have the page here - [a href=\"http://ohrl.forthestudentsbookstore.com/Elkin/upload.php\" target=\"_blank\"]http://ohrl.forthestudentsbookstore.com/Elkin/upload.php[/a]I wrote this sql state to make sure there is something there...which you can there is something therehere the code revised....half of it is the test query[code]<?phpinclude_once('Scripts/conn/cid.php'); $sql = "SELECT ra_name FROM staff_elkin"; $result = mysql_query($sql) or die('Query failed. ' . mysql_error());if (mysql_num_rows($result) > 0) { while ($row = mysql_fetch_array($result)) { $title = $row['title']; ?> <select name="raname" id="raname"> <?php echo("<option value='$title'>$title</option>"); ?> </select> <?php }} include_once('Scripts/close/end.php');?>//THIS IS THE QUERY CODE TO TEST THE DATABASE<?phpinclude_once('Scripts/conn/conn.php'); $rowsPerPage = 300;$pageNum = 1;if(isset($_GET['page'])){$pageNum = $_GET['page'];}$offset = ($pageNum - 1) * $rowsPerPage;$query = "SELECT ra_name FROM staff_elkin"; $result = mysql_query($query) or die('Error, query failed');echo '<table border="0">';while(list($raname) = mysql_fetch_array($result)){echo "<tr><td>$raname <em>made the request:</em></td></tr>";}echo '</table>'; echo '<br>';include_once('Scripts/close/end.php');?>[/code][/quote]in your sql you select column 'ra_name' and in line [code]$title = $row['title'];[/code]you look in column 'title'. Take look at this. Quote Link to comment https://forums.phpfreaks.com/topic/8913-help-populating-combo-box/#findComment-32777 Share on other sites More sharing options...
ichversuchte Posted May 2, 2006 Author Share Posted May 2, 2006 Ok...i changed that and it filled the combo box, but it create a combobox for everyname. It didn't put them all in one and that is what i wanted?i appreciate your help... Quote Link to comment https://forums.phpfreaks.com/topic/8913-help-populating-combo-box/#findComment-32779 Share on other sites More sharing options...
sasa Posted May 2, 2006 Share Posted May 2, 2006 [!--quoteo(post=370721:date=May 3 2006, 12:49 AM:name=ichversuchte)--][div class=\'quotetop\']QUOTE(ichversuchte @ May 3 2006, 12:49 AM) [snapback]370721[/snapback][/div][div class=\'quotemain\'][!--quotec--]Ok...i changed that and it filled the combo box, but it create a combobox for everyname. It didn't put them all in one and that is what i wanted?i appreciate your help...[/quote]move line [code]<select name="raname" id="raname">[/code]before and line[code]</select>[/code]after while loop Quote Link to comment https://forums.phpfreaks.com/topic/8913-help-populating-combo-box/#findComment-32782 Share on other sites More sharing options...
ichversuchte Posted May 2, 2006 Author Share Posted May 2, 2006 Ok i did that and the code looks like this revised...That fixed the double combobox problem, but now its only filling one name into the box.[code]<?phpinclude_once('Scripts/conn/cid.php'); $sql = "SELECT ra_name FROM staff_elkin"; $result = mysql_query($sql) or die('Query failed. ' . mysql_error());if (mysql_num_rows($result) > 0) { while ($row = mysql_fetch_array($result)) { $title = $row['ra_name']; } ?> <select name="raname" id="raname"> <?php echo("<option value='$title'>$title</option>"); ?> </select> <?php } include_once('Scripts/close/end.php');?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/8913-help-populating-combo-box/#findComment-32787 Share on other sites More sharing options...
sasa Posted May 2, 2006 Share Posted May 2, 2006 try[code]<?phpinclude_once('Scripts/conn/cid.php'); $sql = "SELECT ra_name FROM staff_elkin"; $result = mysql_query($sql) or die('Query failed. ' . mysql_error());if (mysql_num_rows($result) > 0) { echo '<select name="raname" id="raname">';while ($row = mysql_fetch_array($result)) { $title = $row['ra_name']; echo("<option value='$title'>$title</option>"); } echo '</select>'; } include_once('Scripts/close/end.php');?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/8913-help-populating-combo-box/#findComment-32793 Share on other sites More sharing options...
ichversuchte Posted May 2, 2006 Author Share Posted May 2, 2006 When i used that code, it did the same thing. only filled one combobox.... Quote Link to comment https://forums.phpfreaks.com/topic/8913-help-populating-combo-box/#findComment-32799 Share on other sites More sharing options...
sasa Posted May 2, 2006 Share Posted May 2, 2006 [!--quoteo(post=370741:date=May 3 2006, 01:11 AM:name=ichversuchte)--][div class=\'quotetop\']QUOTE(ichversuchte @ May 3 2006, 01:11 AM) [snapback]370741[/snapback][/div][div class=\'quotemain\'][!--quotec--]When i used that code, it did the same thing. only filled one combobox....[/quote]I edit previous post Quote Link to comment https://forums.phpfreaks.com/topic/8913-help-populating-combo-box/#findComment-32800 Share on other sites More sharing options...
.josh Posted May 2, 2006 Share Posted May 2, 2006 the <select> </select> tags should be outside of the while loop. if (blah) { <select blah>while (blah) { <option blah></option>}</select>you don't want to loop the select, as this will make a box on each iteration. you only want to iterate the options. Quote Link to comment https://forums.phpfreaks.com/topic/8913-help-populating-combo-box/#findComment-32801 Share on other sites More sharing options...
ichversuchte Posted May 2, 2006 Author Share Posted May 2, 2006 it worked....thank you:D Quote Link to comment https://forums.phpfreaks.com/topic/8913-help-populating-combo-box/#findComment-32802 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.