Reputation:
I am trying to create a drop down menu that will display Project Names off all Projects present in the table Project in the database Testing... The drop down is created but it is not accessing the database and retrieving the required data... The code is as follows:
<html>
<head>
<title>Dynamic Drop Down List</title>
</head>
<form id="form1" name="form1" method="post" action="<?php echo $PHP_SELF; ?>">
Project List :
<select Project Name='NEW'>
<option value="">--- Select ---</option>
<?
$serverName = "Swagatha-PC";
$connectionInfo = array( "Database"=>"Testing");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn )
{
echo "Connection established.<br />";
}
else
{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
if (isset ($select)&&$select!="")
{
$select=$_POST ['NEW'];
}
?>
<?
$query=mysql_query("select ProjectName from dbo.Project");
$menu=" ";
while($row = sqlsrv_fetch_array($query))
{
$menu ="<option>" . $row["ProjectName"] . "</option>";
}
?>
</select>
<input type="submit" name="Next" value="Select" />
</form>
</body>
</html>
What Do i do to fix this??
Upvotes: 0
Views: 47
Reputation: 54831
The solution is
while($row = sqlsrv_fetch_array($query))
{
echo "<option>" . $row["ProjectName"] . "</option>";
}
See? You output data with echo
instead assinging data to variable.
And as @FirstOne noticed using mysql_query
with sqlsrv_connect
is an error too. I hope it's just your typo:
$query=sqlsrv_query($conn, "select ProjectName from dbo.Project");
As another sidenote
if (isset ($select)&&$select!="")
{
$select=$_POST ['NEW'];
}
this if
will always be false, because you first check $select
and then define it. It definitely should be:
if (isset($_POST['NEW']) && $_POST['NEW'] != "")
{
$select = $_POST['NEW'];
}
Upvotes: 1
Reputation: 273
Here is you fixed code. You can try it
<html>
<head>
<title>Dynamic Drop Down List</title>
</head>
<form id="form1" name="form1" method="post" action="<?php echo $PHP_SELF; ?>">
Project List :
<select Project Name='NEW'>
<option value="">--- Select ---</option>
<?php
$serverName = "Swagatha-PC";
$connectionInfo = array( "Database"=>"Testing");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn )
{
echo "Connection established.<br />";
}
else
{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
if (isset ($select)&&$select!="")
{
$select=$_POST ['NEW'];
}
$query=sqlsrv_query("select ProjectName from dbo.Project");
$menu=" ";
while($row = sqlsrv_fetch_array($query))
{
echo "<option>" . $row["ProjectName"] . "</option>";
}
?>
</select>
<input type="submit" name="Next" value="Select" />
</form>
</body>
</html>
Upvotes: 0