Hi

i m trying to get the values from database and put it into the select options..
i m using code-

<?php
$con = mysql_connect("localhost","cdccpl","d123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
					  mysql_select_db("cdccpl_aus", $con);
$result = mysql_query("SELECT * FROM bak_bill_cust");
 while( $row = mysql_fetch_array( $result ) ):
 $option = $row["cust_code"];

echo "<option value='$row[cust_code]'>$row[cust_code]</option>";
endwhile;
 mysql_close($con);?>

but i m getting displayes with the values as--

Customer auswidestonemanpnicholsonllwmplnewuserguestanftasturnbullanglic

means values getting displayed one after another and not in option..

why so..
plz help

Recommended Answers

All 6 Replies

echo your option tags inside of a select tag like this:

<select name="test" id="test">
	<option value="value 1">option 1</option>
	<option value="value 2">option 2</option>
	<option value="value 3">option 3</option>
</select>

Well firstly

$result = mysql_query("SELECT * FROM bak_bill_cust");
 while( $row = mysql_fetch_array( $result ) ):
 $option = $row["cust_code"];

echo "<option value='$row[cust_code]'>$row[cust_code]</option>";
endwhile;

>>>

$result = mysql_query("SELECT * FROM bak_bill_cust");
while( $row = mysql_fetch_assoc( $result ) )
{
    $option = $row["cust_code"];
    echo "<option value='$option'>$option</option>";
}

I actually wasn't even aware PHP could use that format for while loops but I wouldn't advise its use given that it completely breaks the Perl-like syntax and just makes for ugly code.

I actually wasn't even aware PHP could use that format for while loops

Yeah.. I wasn't aware too.. :)

Not sure if your porblem is solved yet, but here is something I use all the time for drop downs.
First get your connection string going. preferably using an include.
then use the following code, adapting statement to your needs:

<?php
$result = mysql_query("SELECT * FROM users ORDER BY username ASC") or 
die(mysql_error());
	$sticky= '';
	if (isset($_POST['id']))
	$sticky = ($_POST['id']);
	$pulldown1 = '<select name="id">';
	$pulldown1 .= '<option></option>';
	while($row = mysql_fetch_array($result))
		{
		if($row['id'] == $sticky) {
		$pulldown1 .= "<option selected value=\"{$row['id']}\">
			{$row['username']}&nbsp;-&nbsp;
                                                {$row['id']}				
                                                </option>\n";
                                } else {
                                $pulldown1 .= "<option value=\"{$row['id']}\">					{$row['username']}&nbsp;-&nbsp;
                                                {$row['id']}
                                                </option>\n";
                                           }
                                }
                               $pulldown1 .= '</select>';
                                echo $pulldown1;	
            ?>

This works well for holding the users selected values when error checking as well...
hope this helps

Hi all..
thanks for reply..

Problem is solved..

try

$sql = "SELECT * FROM bak_bill_cust";
$query = mysql_query($sql) or die('Error: ' . mysql_error());
$select = '<select name="test">';
$select .= '<option value=""></option>';
while ($row = mysql_fetch_assoc($query)) {
$select .= '<option value="' . $row['cust_code'] . '">' . $row['cust_code'] . '</option>';
}
$select .= '</select>';
echo $select;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.