user7390100
user7390100

Reputation:

PHP- Unable to Access Data from DB in dropdown menu

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>

Dropdown Error

What Do i do to fix this??

Upvotes: 0

Views: 47

Answers (2)

u_mulder
u_mulder

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

Farhad Hossen
Farhad Hossen

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

Related Questions