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

2
  • mysql_query and sqlsrv_fetch_array? hmm.. Not even a Can I mix MySQL APIs in PHP?. But for real, you're probably looking for sqlsrv_query Commented Jun 7, 2017 at 16:50
  • echo is missing in the while loop Commented Jun 7, 2017 at 16:52

2 Answers 2

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'];
}
Sign up to request clarification or add additional context in comments.

5 Comments

Besides from connecting with sqlsrv_connect and running mysql_query xD
Yeah, mentioned it, I suppose now we can close this as dupe(
Nope its still not working..Have made both the changes
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: <select Project Name='NEW'> what's Project supposed to mean? 3: $select=$_POST ['NEW']; seems like a self fulfilling prophecy - though it will never be fulfilled xD since to select something, you'll need to have selected something (new is the name of the select itself).
@FirstOne It doesn't seem to be working still..Not gttng wer i hv gone wrong
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>

2 Comments

Remove = after echo
Thanks for suggested me @u_mulder

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.