#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    30
    Rep Power
    3

    Populate HTML form from mySQL DB


    I have an html form with a dropdown box that I would like to populate from an mysql db table. The code below is what I currently have which is not working at all. I have not found a good tutorial on how to accomplish this yet so I am not sure how to really do this. Any help as far as links to a site or explanations would be greatly appreciated.

    Code:
    <tr>
    	    <td align="right" width="25%">Register For A Course:</td>
    	    <td align="left"><select name="course" size="1" margin="0" class="course" id="course">
    						
    <?php
    $db = mysql_connect('127.0.0.1', 'root', '', 'dbfeedback');
    $query = "SELECT membertype FROM membershiptype";
    
    $result = mysql_query($query);
    
    $dropdown = "<select name='membershiptype'>";
    
    while($row = mysql_fetch_assoc($result)) {
    	$dropdown .= "\r\n<option value='{$row['membertype']}'>{$row['membertype']}</option>";}
    	$dropdown .="\r\n</select>";
    	echo $dropdown;
    	mysql_close($db);
    ?>
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,122
    Rep Power
    9398
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    30
    Rep Power
    3
    In the dropdown box it shows the text {row['membertype']} and then outside the dropdown box it shows "; echo $dropdown; mysql_close($db); ?>.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    30
    Rep Power
    3
    Sort of found a problem. I actually had a <select> inside of the php and outside. When I removed the one from outside the php the entire code from while down through ?> came outside of the dropdown box and there is not a drop down box anymore.
  8. #5
  9. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,122
    Rep Power
    9398
    How sure are you that you installed PHP correctly? Is that code in a .php file? Does the URL you're at start with "http://"?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    30
    Rep Power
    3
    Originally Posted by requinix
    How sure are you that you installed PHP correctly? Is that code in a .php file? Does the URL you're at start with "http://"?
    I actually forgot to save it as php file. When opening as a php file I get the following error.

    Code:
    Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in G:\EasyPHP-5.3.9\www\Week4\membership.php on line 120
    Line 20 is

    Code:
    while($row = mysql_fetch_assoc($result)) {
  12. #7
  13. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,122
    Rep Power
    9398
    Parameter 1 is $result, and if it's a boolean that probably means it's false. Which means that the query failed for some reason.

    In other words,
    Code:
    SELECT membertype FROM membershiptype
    there's something wrong with that. I can tell you it's not a syntax error so it must be something else.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    30
    Rep Power
    3
    I ran the query in MySQL and it returned all the records for the table. Thats why it is confusing me.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    30
    Rep Power
    3
    When echoing mysql_errno($db) I get the below response so I guess it is not connecting to my database for some reason.

    Code:
    1046: No database selected
    Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in G:\EasyPHP-5.3.9\www\Week4\membership.php on line 120
    I then noticed I had

    $db = mysql_connect instead of mysqli_connect. Changing that gets rids of the 1046 error, but now i get the following. Also the drop down is back. So I am getting closer. Not sure what other kinds of error procesing i can throw in to see if I can find other possible errors.

    Code:
    Warning: mysql_errno() expects parameter 1 to be resource, object given in G:\EasyPHP-5.3.9\www\Week4\membership.php on line 119
    
    Warning: mysql_error() expects parameter 1 to be resource, object given in G:\EasyPHP-5.3.9\www\Week4\membership.php on line 119
    :
    Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in G:\EasyPHP-5.3.9\www\Week4\membership.php on line 120
  18. #10
  19. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,122
    Rep Power
    9398
    [edit] You can't mix mysql and mysqli. For now just stay with the mysql_* functions. When you're comfortable with the basics you can use mysqli. [/edit]

    Ah, missed it.
    PHP Code:
    mysql_connect('127.0.0.1''root''''dbfeedback'
    The fourth argument to mysql_connect() is not the name of the database. You need mysql_select_db for that.
    PHP Code:
    $db mysql_connect('127.0.0.1''root''');
    mysql_select_db('dbfeedback'); 
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    30
    Rep Power
    3
    That worked perfectly. You are awesome. You have any links that can explain how to concat three fields from each record and then actually display that. For instance instead of just having membertype I would have membertype | duration | price?

    None of this is actually part of my homework as I really only had to type in with html the different options but I am trying to learn ways that would probable be better for future use.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    30
    Rep Power
    3
    Nevermind on the concat part. Got that figured out. Might possibly have a question about how to deconcat if that is even possible for when I write my selection back to the database but for now I am just happy.

IMN logo majestic logo threadwatch logo seochat tools logo