62512

populate dropdown is not working

Question:

I am trying to populate the dropdown using the following code. My database connection string works fine. But the dropdown is empty for some reason.

<? mysql_connect("localhost", "user", "pass") mysql_select_db("mydatabase"); $sql = "SELECT code1 FROM table1"; $result = mysql_query($sql) or die(mysql_error()); ?>

<select name="vers"> <?php while($row = mysql_fetch_array($result)){ echo "option value=\"".$row['code1']."\">".$row["code1"]."</option>"; } ?> </select>

Answer1:

Well to start with your html is invalid

echo "<option value=\"".$row['code1']."\">".$row["code1"]."</option>";

You're missing the opening < on the option tag.

Answer2:

should be

echo "<option value=\"".$row['code1']."\">".$row["code1"]."</option>";

Answer3:

You missed one opening bracket (<) before the <em>option value=</em> thing

Answer4:

I was having the same problem, so I went online and did some research. I found a video on Youtube called "Get dropdown items from mysql database". The URL: <a href="http://www.youtube.com/watch?v=nT0Wjt8ukZk" rel="nofollow">Get dropdown items from mysql</a>

Basically, it has you create three files:

<ul><li>a file to connect to the database</li> <li>a file to process all your PHP</li> <li>a file to actually display on the browser window</li> </ul>

This follows the basic MVC (Model View Controller) file format.

After you create and connect to the database, it will show you how to write the PHP to state your query and assign it to a value to be displayed in a drop-down list.

Finally, it shows how to write the HTML to display the list with the populated data.

I would strongly suggest creating the table on your database in the video. This made it very easy for me to change what needed to be changed for my specific problem later, after I had finished writing the code.

Recommend

  • Dojo - how to submit data using a Dialog form
  • How can retrieve string formData js in c#
  • Use VBA to click on a button in IE
  • Uncaught TypeError: Object [object Object] has no method 'fancybox'
  • Notice: Undefined index: image - unable to find the error
  • Gephi's java default method not implemented in C# with an ikvm-from dll library
  • Can't connect using mysql_connect to Database in ipage.com hosting?
  • How to get the second value in a dropdown box based on the selected one in the first dropdown box
  • output json array in php
  • Storing data from SQL in array
  • Passing parameter through “window.location.href”
  • JSON - slashes not escaping
  • PDO error when wrong host name
  • Randomizing -and remembering that randomisation- multiple choice questions in php
  • Add dynamic data to line chart from mysql database with highcharts
  • How does this usort cmp function actually work?
  • Stop Bash Script if Hive Fails
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • How do I access an unhandled exception in an MVC Error view?
  • Google Custom Search with transparent background
  • Debugging ASP.NET on a built-in web server suddenly stops
  • FB SDK and cURL: Unknown SSL protocol error in connection to graph.facebook.com:443
  • vba code to select only visible cells in specific column except heading
  • Display Images one by one with next and previous functionality
  • Upload files with Ajax and Jquery
  • Do I've to free mysql result after storing it?
  • Unanticipated behavior
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • A cron job substitute?
  • json Serialization in asp
  • python regex in pyparsing
  • Android Google Maps API OnLocationChanged only called once
  • Can't mass-assign protected attributes when import data from csv file
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • Conditional In-Line CSS for IE and Others?
  • To Get the radio button value in ruby on rails
  • How can I use threading to 'tick' a timer to be accessed by other threads?