Jump to content


Photo

help populating combo box


  • Please log in to reply
13 replies to this topic

#1 ichversuchte

ichversuchte
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 02 May 2006 - 07:26 PM

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

<?php
include_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');
?>


This is what the connection to the databse looks like

[code=auto:0]


<?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

#2 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 02 May 2006 - 07:45 PM

inser
$xxxx=mysql_select_db($db);
in your code


#3 ichversuchte

ichversuchte
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 02 May 2006 - 08:14 PM

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



<?php
include_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');
?>

THIS IS THE CODE FOR THIS FILE - include_once('Scripts/conn/cid.php');


<?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


#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 02 May 2006 - 08:48 PM

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.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 ichversuchte

ichversuchte
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 02 May 2006 - 09:39 PM

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 there

here the code revised....half of it is the test query



<?php
include_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


<?php
include_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');
?>



#6 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 02 May 2006 - 10:44 PM

[!--quoteo(post=370708:date=May 2 2006, 11:39 PM:name=ichversuchte)--][div class=\'quotetop\']QUOTE(ichversuchte @ May 2 2006, 11:39 PM) View Post[/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 there

here the code revised....half of it is the test query
<?php
include_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
<?php
include_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');
?>

[/quote]
in your sql you select column 'ra_name' and in line
$title = $row['title'];
you look in column 'title'. Take look at this.

#7 ichversuchte

ichversuchte
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 02 May 2006 - 10:49 PM

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...

#8 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 02 May 2006 - 10:53 PM

[!--quoteo(post=370721:date=May 3 2006, 12:49 AM:name=ichversuchte)--][div class=\'quotetop\']QUOTE(ichversuchte @ May 3 2006, 12:49 AM) View Post[/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
<select name="raname" id="raname">
before and line
</select>
after while loop

#9 ichversuchte

ichversuchte
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 02 May 2006 - 10:59 PM

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.




<?php
include_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');
?>


#10 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 02 May 2006 - 11:05 PM

try
<?php
include_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');
?>


#11 ichversuchte

ichversuchte
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 02 May 2006 - 11:11 PM

When i used that code, it did the same thing. only filled one combobox....



#12 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 02 May 2006 - 11:13 PM

[!--quoteo(post=370741:date=May 3 2006, 01:11 AM:name=ichversuchte)--][div class=\'quotetop\']QUOTE(ichversuchte @ May 3 2006, 01:11 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
When i used that code, it did the same thing. only filled one combobox....
[/quote]
I edit previous post

#13 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 02 May 2006 - 11:15 PM

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.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#14 ichversuchte

ichversuchte
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 02 May 2006 - 11:16 PM

it worked....thank you

:D




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users