naveendk.55 Posted September 18, 2011 Share Posted September 18, 2011 Hi I'm new to php and trying to get all the list of names in a drop down menu from database. I tried the below code and received error "Warning: mysql_fetch_array() expects parameter 1 to be resource". Also there is a unique number assigned to each name in the database. I want to display this unique number in ID filed once the appropriate name is selected. Help !!!!!. <table id="table"> <tr> <td background-color="green"> 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 FROM empdata 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="sapid" id="sapid" value="" /> </td> </tr> </table> <br/> <br/> <center> <input name="submit" type="submit" value="Submit Details" /> <input name="Reset" type="reset" value="Reset"> </center> </form> Quote Link to comment Share on other sites More sharing options...
TrueColors Posted September 18, 2011 Share Posted September 18, 2011 mysql_query returns a resource on success and false on failure. As the parameter passed is not a resource states that your query is indeed failing due to an error of some kind. Please use the following format: $result = mysql_query ( "SELECT Name FROM empdata ORDER BY Name ASC" ) or trigger_error ( mysql_error ( ) ); What this does, if an error were to occur it'll throw an error. The key function here is mysql_error. Quote Link to comment Share on other sites More sharing options...
naveendk.55 Posted September 18, 2011 Author Share Posted September 18, 2011 I tried your function and triggered two errors as you said. a) "Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\projectphp\newEmptyPHPWebPage1.php on line 30". Line 30 on the code is : while ($row = mysql_fetch_array($result)) { b) "Notice: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'database' at line 1 in C:\wamp\www\projectphp\newEmptyPHPWebPage1.php on line 29" .. Line 29 is mentioned below. $result = mysql_query("SELECT Name FROM database") or trigger_error ( mysql_error ( ) ); I think syntax wise it is correct. Let me know if I'm making any mistakes still. Thanks. Quote Link to comment Share on other sites More sharing options...
TrueColors Posted September 18, 2011 Share Posted September 18, 2011 Your use of "database" is not allowed, because it's part of MySQL syntax. You can prevent this from happening by using back ticks around the table name eg: `database` or changing the table name to something different. Quote Link to comment Share on other sites More sharing options...
naveendk.55 Posted September 19, 2011 Author Share Posted September 19, 2011 I tried and also included some additional steps to get the desired results. Now, I'm able to get the names in dropdown list. However, unique ID is not displaying after selecting name from the drop down list. I tried the below code. <html> <head> <title>Form IE</title> <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> </head> <body id="body"> <br/><br/> <form name="audit" method="POST" action=""> <table id="table"> <tr> <td background-color="green"> 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") or trigger_error ( mysql_error ( ) ); 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="sapid" id="sapid" value="" onchange="updateID()" /> </td> </tr> </table> <br/> <br/> <center> <input name="submit" type="submit" value="Submit Details" /> <input name="Reset" type="reset" value="Reset"> </center> </form> </div> </div> </body> </html> Quote Link to comment Share on other sites More sharing options...
TrueColors Posted September 19, 2011 Share Posted September 19, 2011 You will have to explain what it is you are trying to achieve. Quote Link to comment Share on other sites More sharing options...
naveendk.55 Posted September 19, 2011 Author Share Posted September 19, 2011 Sure, I have a table in mysql with two columns (userid and name). I want to display all names in drop down menu. Once the end user select name from drop down menu, the userid should be display automatically in the next textbox filed available. I tried the above code to get the userid once the name is selected. It shows all the name from database but not theuser id once the name is selected. I hope it is clear now. Quote Link to comment Share on other sites More sharing options...
TrueColors Posted September 19, 2011 Share Posted September 19, 2011 Try something like this. The value in the dropdown is the ID of the user. <script type="text/javascript"> function showid ( val ) { document.forms[0].elements['id'].value = val; } </script> <form> <select onchange="showid(this.value)"> <option value="-1" selected disabled>Select One</option> <option value="1">Username 1</option> <option value="2">Username 2</option> </select> <input type="text" name="id" /> </form> Quote Link to comment Share on other sites More sharing options...
naveendk.55 Posted September 20, 2011 Author Share Posted September 20, 2011 Hi, please read my post. I tried my best to explain you in details. Both the values, name and the unique ID, is displayed from database. If I select the name, its related Unique ID should be displayed. Your (TrueColors) code didn't show both the values. Quote Link to comment Share on other sites More sharing options...
TrueColors Posted September 20, 2011 Share Posted September 20, 2011 From my understanding, you have a list of Names and when you select one - you want the relevant ID to be in the textbox. That is exactly what I did. The value attribute contains the ID of the username. Quote Link to comment Share on other sites More sharing options...
naveendk.55 Posted September 20, 2011 Author Share Posted September 20, 2011 Yes Truecolor, but I want to display the results in two different (select and textbox) so it is little different that what you shows. However, I resolved this issue by using scripting and assigning ID as value for select option. Thanks for your helps and postings. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.