[HELP] Display data from mysql to dropdown list

Discussion in 'PHP' started by huseinbandi, Dec 16, 2011.

  1. #1
    Hey guys.. i need your help. i'm failed to display data from mysql into dropdown list. well, this is my code..

    connection.php
    index.php
    mainpost.php
     
    huseinbandi, Dec 16, 2011 IP
  2. blackemerald

    blackemerald Member

    Messages:
    86
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #2
    I think it because <br> tag at echo "$Reflinks[ref_categories_id]==$Category[id]<br>\n";
    Try your code without it.
     
    blackemerald, Dec 16, 2011 IP
  3. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #3
    This line
    
    echo "$Reflinks[ref_categories_id]==$Category[id]<br>\n";
    
    PHP:
    should be generating an error, since there's no variable $Reflinks at that point. (And the syntax is all wrong - what are you trying to echo there? (The best you can get from it is true or false, but not that way.)
     
    Rukbat, Dec 16, 2011 IP
  4. huseinbandi

    huseinbandi Well-Known Member

    Messages:
    1,060
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #4
    can you give me a sample code??please..
     
    huseinbandi, Mar 5, 2012 IP
  5. nabil_kadimi

    nabil_kadimi Well-Known Member

    Messages:
    1,065
    Likes Received:
    69
    Best Answers:
    0
    Trophy Points:
    195
    #5
    Remove this line all together
    echo "$Reflinks[ref_categories_id]==$Category[id]<br>\n";
    PHP:
    And write any error messages your are getting.
     
    nabil_kadimi, Mar 5, 2012 IP
  6. prabhunimish

    prabhunimish Member

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    46
    #6
    See if this code works :

    
    
    
    
    
    <section id="mainpost">
    <h3>Add or modify a listing</h3>
    <a href="#">Test Link</a>
    <form method="post" action="<?php $_SERVER['PHP_SELF']?>">
    <label for="Category">Topic Category</label>
    
    
    <?
    	$hostUrl="localhost";
    	$userName="root";
    	$password="";
    
    
    	$connectID=mysql_connect($hostUrl, $userName, $password) or die("sorry, can't connect to database");
    
    
    	mysql_select_db("database", $connectID) or die("unable to connect database");
    	mysql_close($connectID);
    
    
    
    
    	print '<select name="category">'."\n"; 
    	print '<option value="none">Select a Catagory</option>'."\n"; 
    	
    	$sql = 'SELECT * FROM ref_categories';
    	$Categories = mysql_query($sql,$connectID); 
    	while ($Category = mysql_fetch_assoc($Categories))
    	{ 
    		//echo "$Reflinks[ref_categories_id]==$Category[id]<br>\n";
    		print '<option value="'.$Category['id'].'">'.$Category['category_name']."</option>\n";
    	}
    	
    	print '</select>'."<br />\n"; 
    ?>
    <!-- topic_name -->
    <label for="topic_name">Topic Name</label>
    <input id="topic_name" name="topic_name" type="text" value="" />
    
    
    <!-- topic_description -->
    <label for="topic_description">Topic Description</label>
    <textarea name="topic_description"></textarea>
    
    
    <!-- topic_url -->
    <label for="topic_url">Topic URL</label>
    <input id="topic_url" name="topic_url" type="text" value="" />
    
    
    <input type="submit" name="submitted" value="submit" />
    </form> 
    </section>
    <!-- end mainpost -->
    
    Code (markup):

    I just commented the line causing error and replaced mysql_fetch_array by mysql_fetch_assoc.
     
    prabhunimish, Mar 7, 2012 IP