Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Regular Coder
    Join Date
    Aug 2011
    Posts
    101
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question "Warning: mysql_fetch_array() expects parameter 1 to be resource

    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 !!!!!.


    PHP Code:

    <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" /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

            <input name="Reset" type="reset" value="Reset"> </center>

    </form>

  • #2
    New Coder
    Join Date
    Apr 2010
    Location
    Norfolk, England
    Posts
    63
    Thanks
    1
    Thanked 14 Times in 14 Posts
    Hi naveendk.55

    Addressing the error first, its probably the query thats failing, double check the syntax.

    And as for the second question, am i right in saying you wish the ID field to be automically updated to show the appropriate ID when a name is chosen from the drop down menu?
    //Improvement in coding is iterative, each 'failure' is just the next step on your learning curve, some knowledge and logic can get you a long way.//

  • #3
    Regular Coder
    Join Date
    Aug 2011
    Posts
    101
    Thanks
    7
    Thanked 0 Times in 0 Posts
    About query, let me know if I made any syntax mistake.

    Yes, I want the ID to be popped up automatically after selecting the name.

  • #4
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    You can find out easily yourself - add or die(mysql_error()) inbetween the closing ) of your mysql_query function and the ;. That will kill your code and show you the error:
    PHP Code:
    mysql_query(...) or die(mysql_error()); 

  • #5
    Regular Coder
    Join Date
    Aug 2011
    Posts
    101
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question

    I tried that. It didn't show any error code. However, it is not displaying any datafrom database. Also the ID text box is not displayed on the form.

    PHP Code:

     <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") or die(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=""   />
                </td>

            </tr>

    </table>

        <br/> <br/>

        <center> <input name="submit" type="submit"  value="Submit Details" /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

            <input name="Reset" type="reset" value="Reset"> </center>

    </form>

  • #6
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Is it still displaying the 'expects resource' error? Is the table definitely called empdata? Is the field definitely called Name (not name)?

  • Users who have thanked BluePanther for this post:

    naveendk.55 (09-18-2011)

  • #7
    Regular Coder
    Join Date
    Aug 2011
    Posts
    101
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question

    hi That resolved the error 'expects resource. However, how about getting the unique ID after selecting the user name.

  • #8
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    You need to add it to your query:
    Code:
    SELECT ID, Name FROM empdata ORDER BY Name ASC
    Where ID is your unique id field. Then you can access it inside of your loop with $row['ID'] just like you do with 'Name'.

  • #9
    New Coder
    Join Date
    Apr 2010
    Location
    Norfolk, England
    Posts
    63
    Thanks
    1
    Thanked 14 Times in 14 Posts
    Quote Originally Posted by naveendk.55 View Post
    hi That resolved the error 'expects resource. However, how about getting the unique ID after selecting the user name.
    It may have stopped the error message, but if no data is being retrived it still isn't right. Have you check the table names and column names as BluePanther said?

    As for the ID, assuming every name is unique, there are two ways of doing this really, javascript and PHP.

    With PHP, you would submit the form onchange of the dropdown (which yes, i suppose would use javascript also for the automatic submission, but majority PHP), and pass the name selected as a GET or POST, and then check for it and display the appropriate ID in the text box, using something like this:

    Code:
    <td> ID </td>
    <td colspan='3'> <input type="text" name="sapid" id="sapid" value="<?php 
    $r = mysql_query("select * from empdata where name = '$_GET[chosenName]'");
    $t = mysql_fetch_row($r);
    echo $t['ID'];
    ?>"   />
    </td>
    Where chosenName would be the variable passed in the GET (from the submitted drop down).

    You would also then be free to fill in other form details automatically if you wish, using the data retrived.

    Alternatively you could use javascript.

    Out of the ways to do it, I would suggest creating an array of IDs, and then updating that way.

    something like this script section put in the head should do it:

    Code:
    <script type="text/javascript">
    var ids =  new Array();
    
    window.onload = function() {
        document.getElementById('aname').onchange = updateID;
    
       <?php
       $names = mysql_query("SELECT Name FROM empdata ORDER BY Name ASC") or die(mysql_error());
    
        while ($row = mysql_fetch_array($result)) 
        {
           echo "names[names.length] = $row['Name']";
        }
       ?>  
    
    
    }
    
    function updateID() {
       document.getElementById('sapid').value = ids[document.getElementById('aname').selectedIndex];
    }
    
    </script>
    All freetyped, and untested, so excuse any errors.
    Last edited by thesam101; 09-18-2011 at 08:39 PM.
    //Improvement in coding is iterative, each 'failure' is just the next step on your learning curve, some knowledge and logic can get you a long way.//

  • Users who have thanked thesam101 for this post:

    naveendk.55 (09-20-2011)

  • #10
    Regular Coder
    Join Date
    Aug 2011
    Posts
    101
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question

    I tried the below steps and ID filed is displaying as "undefined". All are working except the ID field.

    Code:
    <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>

  • #11
    New Coder
    Join Date
    Apr 2010
    Location
    Norfolk, England
    Posts
    63
    Thanks
    1
    Thanked 14 Times in 14 Posts
    Hi naveendk.55

    That <script> section I provided should go in the head section like I stated

    i.e. somewhere between the <head> and </head> tags.
    //Improvement in coding is iterative, each 'failure' is just the next step on your learning curve, some knowledge and logic can get you a long way.//

  • #12
    Regular Coder
    Join Date
    Aug 2011
    Posts
    101
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question

    Hi, I tried that and it works perfectly except displaying the unique number after selecting the name from drop down list.

    PHP 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 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" /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

            <input name="Reset" type="reset" value="Reset"> </center>

    </form>

    </div>

    </div>

    </body>

    </html>
    Last edited by naveendk.55; 09-19-2011 at 08:55 PM. Reason: updated

  • #13
    New Coder
    Join Date
    Apr 2010
    Location
    Norfolk, England
    Posts
    63
    Thanks
    1
    Thanked 14 Times in 14 Posts
    Hi naveendk.55

    I'm very sorry, i made a slight error (i did say it was freetyped lol)


    This:

    PHP Code:
    echo "names[names.length] = $row[userid]"
    should be this:

    PHP Code:
    echo "ids[ids.length] = $row[userid]"
    //Improvement in coding is iterative, each 'failure' is just the next step on your learning curve, some knowledge and logic can get you a long way.//

  • #14
    Regular Coder
    Join Date
    Aug 2011
    Posts
    101
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Nope, No output even after changing the code to ids.

  • #15
    New Coder
    Join Date
    Apr 2010
    Location
    Norfolk, England
    Posts
    63
    Thanks
    1
    Thanked 14 Times in 14 Posts
    Quote Originally Posted by naveendk.55 View Post
    Nope, No output even after changing the code to ids.
    Oh dear, i'm sorry, my freetyping seem to have struck twice, this:

    PHP Code:
    while ($row mysql_fetch_array($result)) {
        echo 
    "ids[ids.length] = $row[userid]";

    should be this:

    PHP Code:
    while ($row1 mysql_fetch_array($names)) {
        echo 
    "ids[ids.length] = $row1[userid]";

    If it doesn't work after that change, could you provide the rendered source please? (the code shown when you goto view>source)
    Last edited by thesam101; 09-19-2011 at 11:12 PM.
    //Improvement in coding is iterative, each 'failure' is just the next step on your learning curve, some knowledge and logic can get you a long way.//


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •