Page 1 of 1

database query help

Posted: Mon Sep 12, 2011 6:17 am
by naveendk.55
[text]I am trying to access a value (number called userid) from database and display it in Textbox. This user ID should be depending on the name that customer select from a drop down box above the UserId textbox. The NAME that customer select is also from database.

Any one help me to write the WHERE statement and put that in textbox. Below is the code that I tried but got stuck.
[/text]

Code: Select all


<td> Name </td> 
                    <td> <select name="aname" id="aname" style="width: 147px">
                            <?php
                            $result = mysql_query("SELECT name FROM empdata where role='user' ORDER BY name ASC");
                            while ($row = mysql_fetch_array($result)) {
                                echo "<option>" . $row['name'] . "</option>";
                            }
                            ?>
                        </select></td>
                </tr> 
<tr>
                    <td> ID </td> 
                    <td colspan='3'> <input type="text" name="id" id="id"/>
                    
                        <?php
                            $result = mysql_query("SELECT userid FROM empdata where name= ");
                            
                            ?>
                    </td>


Re: database query help

Posted: Mon Sep 12, 2011 7:16 pm
by twinedev
First, you are not going to get PHP by itself (server side language) to interact with choices made on the client side. You will need to either submit the form back with the name so that it knows what userID to lookup or use javascript (client side language) to change that value when onClick() is fired on the SELECT.

BUT, in looking at this form, what is the task you are trying to obtain? Does the user really need to see the ID, or are you just doing it to try to get the ID passed back when you submit the form? If so just use the following. (I"m keeping it similar to how you have it, but normally I get all data loaded up before outputting code)

Code: Select all

<td>
    <select name="id" id="id" style="width: 147px">
        <?php
            $result = mysql_query("SELECT userid,name FROM empdata where role='user' ORDER BY name ASC");
            if ($result && mysql_num_rows($result)) {
                while ($row = mysql_fetch_assoc($result)) {
                    echo '<option value="' . $row['userid'] . '">' . htmlspecialchars($row['user']) . "</option>\n";
                }
                mysql_free_result($result);
            }
            unset($result);
        ?>
    </select>
</td>
Using the above, when the form is submitted, you will have id of the selected user.

-Greg

Re: database query help

Posted: Mon Sep 19, 2011 7:13 am
by naveendk.55
Hi, I tried the below steps using Javascript and PHP. But the unique ID is showing as undefined in output.

Code: Select all


 <table id="table"> 
            <tr>
                <td background-color="green"> Evaluator Name  </td>
                <td> <input type="text" name="evaluator_name" id="evaluator_name"/> </td>
            </tr>
            <tr>
                <td> Agent Name </td> 
                <td> <select name="aname" id="aname" style="width: 147px">
                        <?php
                        $result = mysql_query("SELECT name, userid FROM empdata where role='user' ORDER BY name ASC");
                        while ($row = mysql_fetch_array($result)) {
                            echo "<option>" . $row['name'] . "</option>";
                        }
                        ?>
                    </select></td>
            </tr> 
            <tr>
                <td> SAP ID </td> 
                <td colspan='3'> <input type="text" name="sapid" id="sapid" value="" onchange="updateID()">
                    <script type="text/javascript">

                        var ids =  new Array();
                        window.onload = function() {
                            document.getElementById('aname').onchange = updateID;

<?php
$names = mysql_query("SELECT userid FROM empdata") or die(mysql_error());

while ($row = mysql_fetch_array($result)) {
    echo "names[names.length] = $row[userid]";
}
?>  

                        }
                        function updateID() {
                            document.getElementById('sapid').value = ids[document.getElementById('aname').selectedIndex];
                        }
                    </script>
                </td>
            </tr> 
        </table>