0

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

  • `mysql_query` and `sqlsrv_fetch_array`? hmm.. Not even a [Can I mix MySQL APIs in PHP?](https://stackoverflow.com/questions/17498216/can-i-mix-mysql-apis-in-php). But for real, you're probably looking for [sqlsrv_query](http://php.net/manual/en/function.sqlsrv-query.php) – FirstOne Jun 07 '17 at 16:50
  • echo is missing in the while loop – Jay Patel Jun 07 '17 at 16:52

2 Answers2

1

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'];
}
u_mulder
  • 54,101
  • 5
  • 48
  • 64
  • Besides from connecting with `sqlsrv_connect` and running `mysql_query` xD – FirstOne Jun 07 '17 at 16:51
  • Yeah, mentioned it, I suppose now we can close this as dupe( – u_mulder Jun 07 '17 at 16:52
  • Nope its still not working..Have made both the changes –  Jun 07 '17 at 16:56
  • I feel like commenting here because this will become more complete, instead of a bunch of answers with each a little info. Sorry if it's trouble for ya ^^. 1: the op doesn't have anything setting `$select` before `if (isset ($select)&&$select!="")`. 2: ` – FirstOne Jun 07 '17 at 17:06
  • @FirstOne It doesn't seem to be working still..Not gttng wer i hv gone wrong –  Jun 13 '17 at 11:26
0

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>
Farhad Hossen
  • 263
  • 1
  • 6