hi

how to polute my drop down using the database content...........

polute ? What do you mean by that ?

try this code it works for me

<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="forum"; // Database name
$tbl_name="test_mysql"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM test_mysql";
$result=mysql_query($sql);

?>

<form name="form1" method="post" action="">
  <select name="Names" id="Names">
<?php

while($rows=mysql_fetch_array($result, MYSQL_ASSOC))

{



echo"<option>". $rows['name']."</option>"; //to display names from database in field called name


}

mysql_close();
?>
 
 
  </select>
</form>
commented: Thanks for the rep man. I am not good enough though. I m still learning. +1

sorry how to retrieve the table content and display in a dropdown

can you please be specific.

Do you want to put content from database into drop down or from a table. If from a table where is the content coming from

thanks

You can do something like tirivamwe has mentioned. Or you can use a function so that you can reuse the code whenever you want.

<?php
$conn=mysql_connect("localhost","root");
mysql_select_db("test");
function dropdown($colname,$tablename){
	$query="select ".$colname." from ".$tablename;
	$result=mysql_query($query);
	$option="";
	while($row=mysql_fetch_array($result)){
		$option.="<option value=".$row[0].">".$row[0]."</option>";
	}
	return $option;
}
?>
<html>
<body>
<form>
<select name='test'>
<?php echo $options=dropdown("Column_name","Table_name"); 
//the column name of table_name which you want to display in the options 
 ?>
</select>
<select name='test1'>
<?php echo $options=dropdown("Column_name1","Table_name2"); 
 ?>
</select>
</form>
</body>
</html>

hi
this works but how to get the dropwown value in the next page..
i tried with $id=$_REQUEST; but this is not working
<form method="get" action="sort2.php">
<?php
$hostname = "localhost";
$username = "";
$password = "";
$dbid = "";
$link=mysql_connect($hostname, $username, $password);
mysql_select_db($dbid) or die("unable to connect");

$sql="SELECT id,name FROM personal";
$result=mysql_query($sql);

?>
<table>

<tr>
<td>Splendor Id</td><td><select name="Names">
<?php

while($rows=mysql_fetch_array($result))

{
echo"<option value=".$rows[0].">". $rows[1]."</option>";
}

mysql_close();
?>


</select></td>
</tr>
<tr>
<td>From</td><td><input type="text" name="from"></td>
<td>To</td><td><input type="text" name="to"></td>
<td><input type="submit" name="submit" value="submit"></td></tr>

</table>
</form>

hi
this works but how to get the dropwown value in the next page..
i tried with $id=$_REQUEST; but this is not working
<form method="get" action="sort2.php">
<?php
$hostname = "localhost";
$username = "";
$password = "";
$dbid = "";
$link=mysql_connect($hostname, $username, $password);
mysql_select_db($dbid) or die("unable to connect");

$sql="SELECT id,name FROM personal";
$result=mysql_query($sql);

?>
<table>

<tr>
<td>Splendor Id</td><td><select name="Names">
<?php

while($rows=mysql_fetch_array($result))

{
echo"<option value=".$rows[0].">". $rows[1]."</option>";
}

mysql_close();
?>


</select></td>
</tr>
<tr>
<td>From</td><td><input type="text" name="from"></td>
<td>To</td><td><input type="text" name="to"></td>
<td><input type="submit" name="submit" value="submit"></td></tr>

</table>
</form>

$id=$_REQUEST should be $id=$_REQUEST

And Please, next time you post your code, place it within [ code] ... [ / code] tags !

sorry.......

thanks

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.