0

Hi All,

I am still trying to piece all of this together and any help would be apprciated.
I have the code (see below) to retrieve data from my sql database. The data being pulled into the dropdown is the user name. There are two additional fields in the database - password and e-mail address.

Now that I have the user name in the dropdown for the end user to select I am not sure how to display the records associated with their selection? Here is what I have so far.

<body>

    <form method="post" action="drop.php">

        <select id="user" name="user">

            <?php

            $mysqlserver="localhost";
            $mysqlusername="root";
            $mysqlpassword="";
            $link=mysql_connect(localhost, $mysqlusername, $mysqlpassword) or die ("Error connecting to mysql server: ".mysql_error());

            $dbname = 'members';
            mysql_select_db($dbname, $link) or die ("Error selecting specified database on mysql server: ".mysql_error());

            $myquery="SELECT username FROM members";
            $myresult=mysql_query($myquery) or die ("Query to get data from members failed: ".mysql_error());

            while ($row=mysql_fetch_array($myresult)) {
            $user=$row[username];
                echo "<option>
                    $user
                </option>";

            }

            ?>

        </select>

    </form>

</body>

Thank you for any help you may be able to give.

3
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by jkulp4
1

Hi jkulp4,

Add a submit button to the bottom of your form. When you select a user from the list then click submit, the username will be sent to drop.php by POST (as you specified in your form tag). In drop.php you can collect that information with $username=$_POST['user'];

Edited by Zagga

Votes + Comments
Worked thank you
2

You will need to check the POST variable to see if a user has been selected, then make an additional call to the database to fetch the related data.

I have also added a javascript call to automatically post the form when the dropdown is selected.

It will probably be easier if you move your <select> tags inside the php code. Then you will be able to print out the other data outside of the dropdown, as per below example.

<form id="userform" method="post" action="drop.php">

    <?php

        $mysqlserver="localhost";
        $mysqlusername="root";
        $mysqlpassword="";
        $link=mysql_connect(localhost, $mysqlusername, $mysqlpassword) or die ("Error connecting to mysql server: ".mysql_error());
        $dbname = 'members';
        mysql_select_db($dbname, $link) or die ("Error selecting specified database on mysql server: ".mysql_error());
        $myquery="SELECT username FROM members";
        $myresult=mysql_query($myquery) or die ("Query to get data from members failed: ".mysql_error());

        echo '<select id="user" name="user" onchange="document.forms[\'userform\'].submit();">';

        while ($row=mysql_fetch_array($myresult)) {
        $user=$row[username];         // NOTE: username should be in quotes here.
            echo "<option>
                $user
            </option>";
        }

        echo '</select>';

        if (isset($_POST['user'])) {
            $r = mysql_query("SELECT password, email FROM members WHERE username='".$_POST['user']."'") or die ("Unable to get user details: ".mysql_error());
            while ($row=mysql_fetch_array($r)) { // there should only ever be 1 result
                // display the data however you want, add some HTML tags to format nicely.
                echo $row['password'];
                echo $row['email'];
            }
        }

    ?>
</form>

Please note that the above code is not tested, but should give you an idea of how to proceed.

Also, it is bad practice to use a POST variable directly in a query string (as I have done here) without parsing it for potential injection code - there are a bunch of functions available to handle this for you, I wanted the example to show a clear proof of concept for the methodology.

0

Thank you both that helped. I have the form working correctly now. Appreciate the help and the ability to learn. :-)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.